RUST_BACKTRACE=1 && cargo run
use std::fs::File; fn main() { let f = File::open("hello.txt"); let f = match f { Ok(f) => f, Err(e) => panic!("Error opening file: {}", e), }; } thread 'main' panicked at 'Error opening file: No such file or directory (os error 2)', src/main.rs:7:19 stack backtrace: 0: rust_begin_unwind at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/panicking.rs:584:5 1: core::panicking::panic_fmt at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/core/src/panicking.rs:142:14 2: demo::main at ./src/main.rs:7:19 3: core::ops::function::FnOnce::call_once at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/core/src/ops/function.rs:248:5 note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
use std::fs::File; use std::io::ErrorKind; fn main() { let path = "hello.txt"; let f = File::open(path); let f = match f { Ok(f) => f, Err(e) => match e.kind() { ErrorKind::NotFound => match File::create(path) { Ok(fc) => fc, Err(e) => panic!("Couldn't create file:{:?}", e) }, oe => panic!("Couldn't create file: {:?}", oe) } }; println!("{:?}", f); }
上方代码挺难阅读的,使用闭包简单改良下试试(闭包后面会讲)
use std::fs::File; use std::io::ErrorKind; fn main() { let path = "hello.txt"; let f = File::open(path).unwrap_or_else(|error| { if error.kind() == ErrorKind::NotFound { File::create(path).unwrap_or_else(|error| { panic!("Error creating file: {:?}", error); }) } else { panic!("Error opening file: {:?}", error); } }); println!("{:?}", f); }
let path = "hello.txt"; let f = File::open(path); let f = match f { Ok(f) => f, Err(e) => panic!("Error opening file: {}", e), }; //等同于 使用unwrap更简洁 let path = "hello.txt"; let f = File::open(path).unwrap();
let path = "hello.txt"; let f = File::open(path); let f = match f { Ok(f) => f, Err(e) => panic!("Error opening file: {}", e), }; //等同于 使用unwrap更简洁 let path = "hello.txt"; let f = File::open(path).unwrap();//不可以自定义信息 //用expect自定义信息 let f = File::open(path).expect("Couldn't open file");
use std::io; use std::fs::File; use std::io::Read; fn main() { let result = read_username(); println!("{:?}", result); } fn read_username() -> Result<String, io::Error> { let f = File::open("hello.txt"); let mut f = match f { Ok(f) => f, Err(e) => return Err(e), }; let mut s = String::new(); match f.read_to_string(&mut s) { Ok(_) => Ok(s), Err(e) => Err(e), } }
use std::io; use std::fs::File; use std::io::Read; fn main() { let result = read_username(); println!("{:?}", result); } fn read_username() -> Result<String, io::Error> { let mut f = File::open("hello.txt")?; //等同于下面注释代码 // let f = File::open("hello.txt"); // let mut f = match f { // Ok(f) => f, // Err(e) => return Err(e), // }; let mut s = String::new(); f.read_to_string(&mut s)?; Ok(s) //等同于下面注释代码 // match f.read_to_string(&mut s) { // Ok(_) => Ok(s), // Err(e) => Err(e), // } }
use std::io; use std::fs::File; use std::io::Read; fn main() { let result = read_username(); println!("{:?}", result); } fn read_username() -> Result<String, io::Error> { //链式调用优化 let mut s = String::new(); File::open("test.txt")?.read_to_string(&mut s)?; Ok(s) }
修改后就可以在main函数里面使用?了