Rust中的错误处理
Result & Panic
这次讲得详细,从错误的来历及简写过程,
都写明白了,
先浅,再深,先深,再浅,
反复之,
学习王道~
use std::fs::File; //use std::io::ErrorKind; fn main() { //panic!("crash and burn"); //let v = vec![1, 2, 3]; //v[99]; /* 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!("Try create new file, but error : {:#?}", e), }, other_error => panic!("There was a problem opening the file: {:#?}", other_error), }, }; let f = File::open("hello.txt").map_err(|error| { if error.kind() == ErrorKind::NotFound { File::create("hello.txt").unwrap_or_else(|error| { panic!("Try create new file, but error : {:#?}", error); }) } else { panic!("There was a problem opening the file: {:#?}", error); } }); */ //let f = File::open("hello.txt").unwrap(); let f = File::open("hello.txt").expect("Failed to open hello.txt"); println!("f value is {:#?}", f); }
Rust中的错误处理的更多相关文章
- Rust 中的类型转换
1. as 运算符 as 运算符有点像 C 中的强制类型转换,区别在于,它只能用于原始类型(i32 .i64 .f32 . f64 . u8 . u32 . char 等类型),并且它是安全的. 例 ...
- Rust中的RefCell和内部可变性
RefCell Rust在编译阶段会进行严格的借用规则检查,规则如下: 在任意给定时间,要么只能有一个可变引用,要么只能有多个不可变引用. 引用必须总是有效. 即在编译阶段,当有一个不可变值时,不能可 ...
- 【译】理解Rust中的闭包
原文标题:Understanding Closures in Rust 原文链接:https://medium.com/swlh/understanding-closures-in-rust-21f2 ...
- 【译】理解Rust中的局部移动
原文标题:Understanding Partial Moves in Rust 原文链接:https://whileydave.com/2020/11/30/understanding-partia ...
- 【译】理解Rust中的Futures (一)
原文标题:Understanding Futures In Rust -- Part 1 原文链接:https://www.viget.com/articles/understanding-futur ...
- 【译】理解Rust中的Futures(二)
原文标题:Understanding Futures in Rust -- Part 2 原文链接:https://www.viget.com/articles/understanding-futur ...
- 【译】深入理解Rust中的生命周期
原文标题:Understanding Rust Lifetimes 原文链接:https://medium.com/nearprotocol/understanding-rust-lifetimes- ...
- 【转】SQL Server -- 已成功与服务器建立连接,但是在登录过程中发生错误
SQL Server -- 已成功与服务器建立连接,但是在登录过程中发生错误 最近在VS2013上连接远程数据库时,突然连接不上,在跑MSTest下跑的时候,QTAgent32 crash.换成IIS ...
- vs2015 编译时错误列表中没有错误,dll却没有生成出来
最近发现vs2015的一个问题, 编译时,错误列表中没有错误,dll却没有生成出来,vs重启也无效 解决: 多次排查发现如果一个类库设置的是framework 4.0版本,但引用了framework4 ...
随机推荐
- 第一周-调用weka算法进行数据挖掘
第一周-调用weka算法进行数据挖掘 简单数据集data.txt @relation weather @attribute outlook {sunny, overcast, rainy} @attr ...
- 第十七周博客作业 <西北师范大学| 周安伟>
第十七周作业 助教博客链接https://home.cnblogs.com/u/zaw-315/ 作业要求链接https://www.cnblogs.com/nwnu-daizh/p/11012922 ...
- [C7] 支持向量机(Support Vector Machines) (待整理)
支持向量机(Support Vector Machines) 优化目标(Optimization Objective) 到目前为止,你已经见过一系列不同的学习算法.在监督学习中,许多学习算法的性能都非 ...
- java中的转义字符(遇到再进一步总结)
一.常见的转义字符转移字符对应的英文是escape character , 转义字符串(Escape Sequence)字母前面加上捺斜线""来表示常见的那些不能显示的ASCII字 ...
- POJ3685Matrix(二分套二分)
传送门 题目大意:N*N的矩阵,a[i][j]=i*i+100000*i+j*j-100000*j+i*j,求矩阵中第K小. N<=5*10^4 题解: 打个表,发现每一列从上往下单调递增. 在 ...
- LOJ6029 [雅礼集训2017]市场
看到区间整除操作,直觉是不会除太多次就变成全 \(1\). 然而现在还有加操作. 我也不知道为什么,当一个节点的 \(\lfloor\frac{mx}{d}\rfloor=\lfloor\frac{m ...
- vue_day05
目录 vue前后端交互: vue 分离前后端交互: vue前端发送请求: vue请求插件--axios: main.js配置: 前端朝后端请求传参方式: django后端返回数据样式: vue配置El ...
- CSS使用知识点
1.空白符 2.字符间距 3.省略号样式 4.水平垂直居中用法 5.CSS角标实现 空格符 1. 相当于键盘按下的空格,区别是 是可以累加的空格,键盘敲下的空格不会累加 2. ...
- HTML连载31-制作一个百度首页
一. 我们制作一个百度首页作为练习,可直接复制该代码保存后缀名为.html来查看 <!DOCTYPE html> <html lang="en"> < ...
- 线程池之ScheduledThreadPoolExecutor线程池源码分析笔记
1.ScheduledThreadPoolExecutor 整体结构剖析. 1.1类图介绍 根据上面类图图可以看到Executor其实是一个工具类,里面提供了好多静态方法,根据用户选择返回不同的线程池 ...