Reputation: 153
In an attempt to write a parser in rust using nom. I ran into this error
error[E0283]: type annotations needed
--> src/parser.rs:8:22
|
8 | let mut parser = delimited(tag("<"), take_until(">"), tag(">"));
| ---------- ^^^^^^^^^ cannot infer type for type parameter `E` declared on the function `delimited`
| |
| consider giving `parser` a type
|
= note: cannot satisfy `_: ParseError<&str>`
note: required by a bound in `delimited`
--> /home/user/.cargo/registry/src/github.com-1ecc6299db9ec823/nom-7.1.1/src/sequence/mod.rs:172:36
|
172 | pub fn delimited<I, O1, O2, O3, E: ParseError<I>, F, G, H>(
| ^^^^^^^^^^^^^ required by this bound in `delimited`
help: consider specifying the type arguments in the function call
|
8 | let mut parser = delimited::<I, O1, O2, O3, E, F, G, H>(tag("<"), take_until(">"), tag(">"));
|
Fair enough, I got this to compile and return what I need it to by changing the line of code to
let mut parser = delimited::<_,_,_,_, VerboseError<_>,_,_,_>(tag("<"), take_until(">"), tag(">"));
However, this seems like a pretty ugly piece of code. Is there a better way of writing this? A way to specify only the type parameter E
rather then listing out all the other parameters with an underscore?
Upvotes: 1
Views: 2051
Reputation: 71605
You can define a function that wraps delimited()
but with more specific generics. Other than that, there isn't much you can do.
Upvotes: 2