感觉写的麻烦 提取PathBuf 成string
fn main() {
let mut condir = String::new();
let k = dirs::config_dir();
println!("The config director is {:?}", k);
if let Some(z) = k {
println!("The Pathbuf is {:?}", z);
if let Some(w) = z.to_str() {
condir.push_str(w);
} else {
condir = "".to_string();
}
} else {
condir = "".to_string();
}
println!("{}", condir);
}
Cargo.toml
[dependencies]
dirs = "2.0.2"
Print:
The config director is Some("/home/jay/.config") The Pathbuf is "/home/jay/.config" /home/jay/.config
1
共 3 条评论, 1 页
评论区
写评论考虑 ?语法糖
谢谢 目前感觉 and_then map_or_else ok_or map_or_else ok_or_else map_or unwrap_or_else ... 这些很晕菜
继续学习
对以下内容的回复:
掌握闭包,Option/Result的一些操作就好了