我正在用 nom crate 学习 Rust,下面是我的程序。
fn get_indent_level(input: &str) -> usize {
let (_, result) = many0_count(alt((tag("L_ "), tag("| "), tag(" "))))(input).unwrap_or_default();
result
}
运行时出现此错误:
error[E0283]: type annotations needed
--> src\lib.rs:149:23
|
149 | let (_, result) = many0_count(alt((tag("L_ "), tag("| "), tag(" "))))(input).unwrap_or_default();
| ^^^^^^^^^^^ cannot infer type for type parameter `E` declared on the function `many0_count`
|
::: C:\Users\user1\.cargo\registry\src\github.com-xxxxxxxxxxxxxxx\nom-6.2.1\src\multi\mod.rs:486:6
|
486 | E: ParseError<I>,
| ------------- required by this bound in `many0_count`
|
= note: cannot satisfy `_: ParseError<&str>`
help: consider specifying the type arguments in the function call
|
149 | let (_, result) = many0_count::<I, O, E, F>(alt((tag("L_ "), tag("| "), tag(" "))))(input).unwrap_or_default();
| ^^^^^^^^^^^^^^
当我添加这样的类型时:
fn get_indent_level(input: &str) -> usize {
let (_, result) = many0_count::<&str, usize, nom::error::ErrorKind, &str>(alt((tag("L_ "), tag("| "), tag(" "))))(input).unwrap_or_default();
result
}
我收到了这个新错误:
error[E0277]: the trait bound `nom::error::ErrorKind: ParseError<&str>` is not satisfied
--> src\lib.rs:149:23
|
149 | let (_, result) = many0_count::<&str, usize, nom::error::ErrorKind, &str>(alt((tag("L_ "), tag("| "), tag(" "))))(input).unwrap_or...
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `ParseError<&str>` is not implemented for `nom::error::ErrorKind`
|
::: C:\Users\user1\.cargo\registry\src\github.com-xxxxxxxxxx\nom-6.2.1\src\multi\mod.rs:486:6
|
486 | E: ParseError<I>,
| ------------- required by this bound in `many0_count`
添加类型和解决问题的正确方法是什么?