I'm having a hard time understanding this error.
In this example, foo1
compiles, but foo2
does not.
use nom::Parser;
fn foo1<'a>() -> impl Parser<&'a str, i8, ()> {
nom::combinator::flat_map(nom::character::complete::u32, |_| {
nom::character::complete::i8
})
}
fn foo2<'a>() -> impl Parser<&'a str, i8, ()> {
nom::character::complete::u32.flat_map(|_| nom::character::complete::i8)
}
The error message I get is:
error[E0277]: the trait bound `nom::FlatMap<fn(_) -> Result<(_, u32), nom::Err<_>> {nom::character::complete::u32::<_, _>}, [closure@log-parser/src/foo.rs:22:44: 22:47], u32>: nom::Parser<&'a str, i8, ()>` is not satisfied
--> log-parser/src/foo.rs:21:18
|
21 | fn foo2<'a>() -> impl Parser<&'a str, i8, ()> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `nom::Parser<&'a str, i8, ()>` is not implemented for `nom::FlatMap<fn(_) -> Result<(_, u32), nom::Err<_>> {nom::character::complete::u32::<_, _>}, [closure@log-parser/src/foo.rs:22:44: 22:47], u32>`
|
= help: the trait `nom::Parser<I, O2, E>` is implemented for `nom::FlatMap<F, G, O1>`
I'm aware that the standalone function nom::combinator::flat_map
returns (in my case) an impl FnMut(&str) -> IResult<&str, i8, ()>
, while the trait method nom::Parser::flat_map
returns a FlatMap<_, _, u32>
. However, I don't know why the error message says that the trait bound is not satisfied. The "help" message even says:
the trait `nom::Parser<I, O2, E>` is implemented for `nom::FlatMap<F, G, O1>`
So what about the trait bound is failing? It would be great to get some advice both in this specific case and the more general "how do I determine why a trait bound is failing?" question.
Thank you!