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 ...
随机推荐
- Python 可执行对象
Python 可执行对象 eval/repr eval eval 可以执行字符串类型的表达式 (或 compile() 创建的代码对象(code object) ) 并返回执行结果 eval(expr ...
- Forethought Future Cup - Elimination Round D 贡献 + 推公式 + 最短路 + 贪心
https://codeforces.com/contest/1146/problem/D 题意 有一只青蛙,一开始在0位置上,每次可以向前跳a,或者向后跳b,定义\(f(x)\)为青蛙在不跳出区间[ ...
- bzoj2115 Xor
题目链接 problem 考虑一个边权为非负整数的无向连通图,节点编号为\(1\) 到 \(N\),试求出一条从 \(1\) 号节点到 \(N\) 号节点的路径,使得路径上经过的边的权值的 \(XOR ...
- 动画展现十大经典排序算法(附Java代码)
0.算法概述 0.1 算法分类 十种常见排序算法可以分为两大类: 比较类排序:通过比较来决定元素间的相对次序,由于其时间复杂度不能突破O(nlogn),因此也称为非线性时间比较类排序. 非比较类排序: ...
- datalab (原发布 csdn 2018年09月21日 20:42:54)
首先声明datalab本人未完成,有4道题目没有做出来.本文博客记录下自己的解析,以便以后回忆.如果能帮助到你就更好了,如果觉得本文没啥技术含量,也望多多包涵. /* * bitAnd - x& ...
- 部署Azure环境Web应用程序不能直接访问JSON文件解决方案
问题: 部署在Azure环境Web应用程序的JSON文件,直接通过浏览器或Web应用访问出现 404 的错误信息. 以下通过Firfox浏览器直接访问JSON文件返回的提示错误信息: “HTML 文档 ...
- centos systemd占用大量内存
不知道为什么,我用vmware做测试用,而且是mini版本,没装什么应用,就是php开发环境,lnmp. 在开发时,内存和swap都爆满,composer包无法安装. 网上搜到解决方法: system ...
- jmeter工具下载及工具功能操作介绍
本博文jmeter介绍的是在windows下使用,linux后期看情况更新,谢谢 简单介绍,想更多了解的去官方,多的很: The Apache JMeter™ application is open ...
- Maven快速入门--Idea版
目录 1.web项目的基本知识 1.1 项目构建 1.1.1传统的构建过程如下: 1.1.2 mavn构建项目 1.2 maven构建项目的优点: 2. 依赖管理 2.1 传统依赖管理 2.1.1 手 ...
- Winform中怎样跨窗体获取另一窗体的控件对象
场景 Winform中实现跨窗体获取ZedGraph的ZedGraphControl控件对象: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/de ...