Macro FromStr
macro_rules! FromStr { ... }
What #[derive(FromStr)] generates
Deriving FromStr only works for enums with no fields
or newtypes, i.e structs with only a single
field. The result is that you will be able to call the parse() method on a
string to convert it to your newtype. This only works when the type that is
contained in the type implements FromStr.
Example usage
# use FromStr;
#
;
assert_eq!;
assert_eq!;
Tuple structs
When deriving FromStr for a tuple struct with one field:
# use FromStr;
#
;
Code like this will be generated:
# ;
Regular structs
When deriving FromStr for a regular struct with one field:
# use FromStr;
#
Code like this will be generated:
#
Enums
When deriving FromStr for an enums with variants with no fields it will
generate a from_str method that converts strings that match the variant name
to the variant. If using a case insensitive match would give a unique variant
(i.e you dont have both a MyEnum::Foo and a MyEnum::foo variant) then case
insensitive matching will be used, otherwise it will fall back to exact string
matching.
Since the string may not match any variants an error type is needed, so the
derive_more::FromStrError will be used for that purpose.
e.g. Given the following enum:
# use FromStr;
#
Code like this will be generated:
#
#