【Rust】可选枚举(三)
环境
- Time 2022-03-25
- Rust 1.59.0
示例
iter_mut
fn main() {
let mut name = Some("JiangBo".to_owned());
println!("{:?}", name.iter_mut().next());
}
and
fn main() {
let name = Some("JiangBo");
println!("{:?}", name.and(Some("Rust")));
}
and_then
fn main() {
let name = Some("JiangBo");
println!("{:?}", name.and_then(|e| Some(e.len())));
}
filter
fn main() {
let name = Some("JiangBo");
println!("{:?}", name.filter(|e| e.is_empty()));
}
or
fn main() {
let name = Some("JiangBo");
println!("{:?}", name.or(None));
}
or_else
fn main() {
let name = Some("JiangBo");
println!("{:?}", name.or_else(|| None));
}
xor
fn main() {
let name = Some("JiangBo");
println!("{:?}", name.xor(None));
}
insert
fn main() {
let mut name = Some("JiangBo");
println!("{:?}", name.insert("Rust"));
}
get_or_insert
fn main() {
let mut name = Some("JiangBo");
println!("{:?}", name.get_or_insert("Rust"));
}
总结
了解了可选枚举中相关的一些方法。