When I try to convert nom’s CompleteStr to a String in named!, I get an error saying it’s returning a Result.
named!(letter_cs<CompleteStr,String>,
map_res!(
alpha,
|CompleteStr(s)| String::from(s)
)
);
will throw the error
error[E0308]: mismatched types
--> src/year2015/day_7.rs:16:1
|
16 | / named!(letter_cs<CompleteStr,String>,
17 | | map_res!(
18 | | alpha,
19 | | |CompleteStr(s)| String::from(s)
20 | | )
21 | | );
| |__^ expected struct `std::string::String`, found enum `std::result::Result`
|
= note: expected type `std::string::String`
found type `std::result::Result<_, _>`
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
map_res!expects a function that returns aResultas the second argument, which is whymap_res!is named like this. You can also see it in its "type" in nom's doc:However,
String::fromdoes not return a result; soString::from(s)is the wrong type formap_res!. Instead, you should use the regularmap!, which has this "type"map!(I -> IResult<I,O>, O -> P) => I -> IResult<I, P>: