11错误处理

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函数里面使用?了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值