环境
- Rust 1.56.1
- VSCode 1.61.2
概念
参考:https://doc.rust-lang.org/stable/rust-by-example/std/option.html
示例
main.rs
fn checked_division(dividend: i32, divisor: i32) -> Option<i32> {
if divisor == 0 {
None
} else {
Some(dividend / divisor)
}
}
fn try_division(dividend: i32, divisor: i32) {
match checked_division(dividend, divisor) {
None => println!("{} / {} failed!", dividend, divisor),
Some(quotient) => {
println!("{} / {} = {}", dividend, divisor, quotient)
}
}
}
fn main() {
try_division(4, 2);
try_division(1, 0);
let none: Option<i32> = None;
let _equivalent_none = None::<i32>;
let optional_float = Some(0f32);
println!(
"{:?} unwraps to {:?}",
optional_float,
optional_float.unwrap()
);
// none unwrap 会 panic
println!("{:?} unwraps to {:?}", none, none.unwrap());
}
总结
了解了 Rust 中标准库中的 Option 的使用。
附录
|
请发表评论