I'm writing a parser using nom
library in rust. When using tuple
I encounter a problem. This snippet works fine:
use std::str;
use nom::bytes::complete::take_while1;
use nom::bytes::complete::{tag, take_while};
use nom::character::complete::{char, multispace0};
use nom::sequence::tuple;
use nom::IResult;
fn identifier(input: &str) -> IResult<&str, &str> {
take_while1(is_ascii_alphanumeric)(input)
}
fn parameter_separator(input: &str) -> IResult<&str, &str> {
let my_parser = tuple((multispace0, char(','), identifier));
let (input, _) = multispace0(input)?;
let (input, _) = char(',')(input)?;
let (input, _) = multispace0(input)?;
Ok((input, ""))
}
But when replacing the identifier
parser with multispace0
parser the compiler ask me to type annotate the my_parser
.
fn parameter_separator(input: &str) -> IResult<&str, &str> {
let parser = tuple((multispace0, char(','), multispace0));
let (input, _) = multispace0(input)?;
let (input, _) = char(',')(input)?;
let (input, _) = multispace0(input)?;
Ok((input, ""))
}
49 | let parser = tuple((multispace0, char(','), multispace0));
| ------ ^^^^^ cannot infer type for type parameter `E` declared on the function `tuple`
| |
| consider giving `parser` a type
What is the difference? Why the second raises error?