ErrorKind::NotFound => File::create("hello.tx").unwrap_or_else(|error| {panic!("Problem creating the file: {:?}", error); }),// 匹配错误原因, 对于文件不存在的错误处理为创建文件other_error_kind =>panic!("Problem opening the file: {:?}", other_error_kind) } });println!("{:...
Err(error)=>matcherror.kind(){ErrorKind::NotFound=>matchFile::create("hello.txt"){Ok(fc)=>fc,Err(e)=>panic!("Problem creating the file: {:?}",e),},other_error=>{panic!("Problem opening the file: {:?
usestd::fs::File;usestd::io::ErrorKind;fnmain(){// 闭包 和 unwrap_or_else 在后面章节会讲到。letgreeting_file=File::open("hello.txt").unwrap_or_else(|error|{iferror.kind()==ErrorKind::NotFound{File::create("hello.txt").unwrap_or_else(|error|{panic!("Problem creating the file: ...
("There is a problem when creating file: {:?}", e), }, other_error => {panic!("There is a problem when open the file: {:?}", other_error); } }, }; } ErrorKind也是一种枚举类型,和Result以及Option不同,ErrorKind需要使用use引入当前的作用域。上面代码中处理了NotFound和other_error两...
Err(error) => match error.kind() { ErrorKind::NotFound => match File::create("hello.txt") { Ok(fc) => fc, Err(e) => panic!("There is a problem when creating file: {:?}", e), }, other_error => { panic!("There is a problem when open the file: {:?}", other_error...
2use std::io::ErrorKind; 3 4fn main() { 5 let f = File::open("hello.txt").unwrap_or_else(|err| { 6 match err.kind() { 7 ErrorKind::NotFound => File::create("hello.tx").unwrap_or_else(|error| { 8 panic!("Problem creating the file: {:?}", error); ...
("error creating {}: {}", path, e), Ok(_) => println!("created {}: OK", path), } let b: bool = Path::new(path).is_dir(); println!("{} exists: {}", path, b); // fs::create_dir_all does create parent folders let r = fs::create_dir_all(path); match r { Err...
use std::fs::File;use std::io::ErrorKind;fn main() {let f = File::open("hello.txt");let f = match f {Ok(file) => file,Err(error) => match error.kind() {ErrorKind::NotFound => match File::create("hello.txt") {Ok(fc) => fc,Err(e) => panic!("Problem creating the...
use std::io::ErrorKind; fn main() { let f = File::open("hello.txt").unwrap_or_else(|error| { if error.kind() == ErrorKind::NotFound { File::create("hello.txt").unwrap_or_else(|error| { panic!("Problem creating the file: {:?}", error); ...
{letc=Cons(4, Rc::clone(&a));println!("count after creating c = {}", Rc::strong_count(&a)); }println!("count after c goes of scope = {}", Rc::strong_count(&a)); } Rc::clone()vs 类型的 clone() 方法 Rc::clone():增加引用,不会执行数据的深度拷贝操作 ...