【发布时间】:2020-11-03 15:31:34
【问题描述】:
我正在尝试使用nom 的tuple 函数。该文档提供了以下示例:
use nom::sequence::tuple;
use nom::character::complete::{alpha1, digit1};
let parser = tuple((alpha1, digit1, alpha1));
当我尝试它时,我得到一个编译错误:
error[E0283]: type annotations needed
--> src/main.rs:20:18
|
20 | let parser = tuple((alpha1, digit1, alpha1));
| ------ ^^^^^ cannot infer type for type parameter `I` declared on the function `tuple`
| |
| consider giving `parser` a type
|
如果我想为变量添加一个类型,它会是什么?我知道它必须是FnMut 的一些变体,但我不确定它究竟是如何工作的。
Cargo.toml
[dependencies]
nom = ">=5.0"
【问题讨论】:
-
如果您实际上从文档中复制了完整的示例,包括
assert_eq!()调用,它编译得很好。使用解析器的行为类型推断提供了所需的额外提示。