nom我用7.1版本进行了测试:
use nom::bytes::complete::tag;
#[test]
fn test() {
let (s, t) = tag("1")("123").unwrap();
}
Run Code Online (Sandbox Code Playgroud)
跑步cargo test给予
use nom::bytes::complete::tag;
#[test]
fn test() {
let (s, t) = tag("1")("123").unwrap();
}
Run Code Online (Sandbox Code Playgroud)
它建议我指定tag::<T, Input, Error>()
我该如何处理这个问题?我还没有完全理解为什么会出现这个问题。
我尝试指定一些类型nom:
error[E0283]: type annotations needed
--> src/main.rs:5:18
|
5 | let (s, t) = tag("1")("123").unwrap();
| ^^^ cannot infer type for type parameter `Error` declared on the function `tag`
|
= note: cannot satisfy `_: ParseError<&str>`
note: required by a bound in `nom::bytes::complete::tag`
--> ~/.cargo/registry/src/github.com-1ecc6299db9ec823/nom-7.1.0/src/bytes/complete.rs:32:29
|
32 | pub fn tag<T, Input, Error: ParseError<Input>>(
| ^^^^^^^^^^^^^^^^^ required by this bound in `nom::bytes::complete::tag`
help: consider specifying the type arguments in the function call
|
5 | let (s, t) = tag::<T, Input, Error>("1")("123").unwrap();
| +++++++++++++++++++
Run Code Online (Sandbox Code Playgroud)
use nom::bytes::complete::tag;
#[test]
fn test() {
let (s, t) = (tag::<_, &str, nom::error::ParseError<&str>>("1")("123")).unwrap();
}
Run Code Online (Sandbox Code Playgroud)
Nom有一个通用错误类型:
解析器的错误类型是通用的,要求它实现该
error::ParseError<Input>特征。
这意味着您的代码需要指定所需的错误类型;问题中的代码没有。
正如编译器建议的那样:考虑在函数调用中指定类型参数。的实现者ParseError之一将是一个很好的起点。Nom 提供了两个:
选其一:
use nom::{bytes::complete::tag, error::Error};
#[test]
fn test() {
let (_s, _t) = tag::<_, _, Error<_>>("1")("123").unwrap();
}
Run Code Online (Sandbox Code Playgroud)
也可以看看: