7 个稳定版本
3.1.1 | 2021 年 9 月 21 日 |
---|---|
3.0.0 | 2021 年 9 月 6 日 |
2.0.0 | 2021 年 7 月 19 日 |
1.1.0 | 2021 年 5 月 12 日 |
1.0.0 | 2020 年 3 月 23 日 |
#613 in 调试
1,983 每月下载量
在 4 个库中使用了(直接使用 3 个)
19KB
283 行
skip_error
skip_error
提供了一个宏,帮助忽略循环中可能发生的 Error
。有关更多信息,请参阅 文档。
Rust 版本要求
skip_error
3.0.0 需要 Rustc 版本 1.54 或更高。
lib.rs
:
此库提供了一个宏,帮助在循环中跳过一个错误,可能还会记录它。
例如,假设您有如下代码。
for string_number in &["1", "2", "three", "4"] {
let number: u32 = match string_number.parse() {
Ok(n) => n,
Err(e) => continue,
};
}
然后您可以使用宏 skip_error!
来这样写。
for string_number in &["1", "2", "three", "4"] {
let number: u32 = skip_error!(string_number.parse());
}
或者更好的是,使用扩展了 Iterator
的 SkipError
特性,并执行以下操作(本质上等同于 [Iterator::flatten()
],但请参见下面的日志功能)。
use skip_error::SkipError;
let numbers: Vec<u32> = ["1", "2", "three", "4"]
.into_iter()
.map(|string_number| string_number.parse())
.skip_error()
.collect();
功能
log
: 使用标准std::log
宏输出日志消息。默认禁用。tracing
: 使用tracing::trace
宏输出跟踪信息。默认禁用。如果同时启用了log
和tracing
,则由于tracing
配置在兼容模式下与标准log
,将忽略log
。
依赖
~105KB