06-优雅地错误处理
优雅地错误处理 — Rust 语言之旅
内容来源 · Tour of Rust / Rust 语言之旅
原文链接: https://tourofrust.com/38_zh-cn.html
优雅地错误处理
Result 如此常见以至于 Rust 有个强大的操作符 ? 来与之配合。
以下两个表达式是等价的:
do_something_that_might_fail()?
match do_something_that_might_fail() {
Ok(v) => v,
Err(e) => return Err(e),
}
示例代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| fn do_something_that_might_fail(i: i32) -> Result<f32, String> {
if i == 42 {
Ok(13.0)
} else {
Err(String::from("this is not the right number"))
}
}
fn main() -> Result<(), String> {
// 看看我们节省了多少代码!
let v = do_something_that_might_fail(42)?;
println!("found {}", v);
Ok(())
}
|