Hello,
I am implementing a small parser for fun, I have the following error message
error[E0308]: mismatched types
--> src/parse.rs:17:31
|
17 | pub fn parse_char(c: char) -> impl Parser<char> {
| ^^^^^^^^^^^^^^^^^ one type is more general than the other
|
= note: expected enum `Result<(&str, _), (&str, ParserError)>`
found enum `Result<(&str, _), (&str, ParserError)>`
As you can see, the compiler is complaining that it found what it expected. I figure out I can be more explicit and return something more specific, but I would still like to understand the problem here.
That is most of the important code
type ParserResult<'a, T> = Result<(&'a str, T), (&'a str, ParserError)>;
#[derive(Debug, PartialEq, Eq)]
struct ParserError(&'static str);
trait Parser<T> {
fn parse<'s>(&self, input: &'s str) -> ParserResult<'s, T>;
}
impl<F, T> Parser<T> for F where F: Fn(&str) -> ParserResult<T> {
fn parse<'s>(&self, input: &'s str) -> ParserResult<'s, T> {
self(input)
}
}
// Can't compile this function.
fn parse_char(c: char) -> impl Parser<char> {
move |input: &str| {
if input.starts_with(c) {
Ok((&input[1..], c))
} else {
Err((input, ParserError("character not found")))
}
}
}
I also made a playground with a minimal reproduction of the error and a little more details.