rust - Rust新手问题,如何处理这个函数中的错误?
问题描述
我正在用 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`
添加类型和解决问题的正确方法是什么?
解决方案
您只需要指定错误类型,因为 nom 可以返回不同的错误类型并且编译器无法推断它,因为错误不会在任何地方使用。您可以使用_
语法让编译器推断其余已知类型。
最简单的方法是在类型上指定IResult
类型,因为它需要的类型少于many0_count()
:
fn get_indent_level(input: &str) -> usize {
let result: IResult<_, _, nom::error::Error<_>> =
many0_count(alt((
tag("L_ "),
tag("| "),
tag(" ")
)))(input);
result.map(|(_, x)| x).unwrap_or_default()
}
这样,您只需要提供 3 种类型,其中 2 种是已知的(输入 - &str
; 和结果 - usize
),因此您可以让编译器使用_
语法推断它们并仅手动编写错误类型。
推荐阅读
- python - 如何根据绘图的 y 值调整太阳图高度
- javascript - Google API 问题
- bayesian - 如何在 ulam 接口中将预测变量矩阵乘以参数向量(重新思考 R 包)
- javascript - 每次提出问题时为画布标签生成唯一的 id 或类?
- powershell - 如何比较两个字符串变量
- kubernetes - GKE 删除部署不会删除副本集
- android - 在 Flutter 项目上运行 Gradle 任务 'assembleDebug' 会导致错误
- alfresco - Alfresco 自定义角色
- ffmpeg - 以相等的时间/间隔从视频创建多个缩略图
- autodesk-forge - 如何检索元素属性(项目元数据)