- 9.1不可修复错误
[profile.release]
panic = 'abort'
主动调用
fn main() {
panic!("crash and burn");
}
例子
fn main() {
let aaa = vec![1,2,3];
aaa[99];
}
使用如下命令运行
#win系统 CMD
set RUST_BACKTRACE=1 && cargo run
#带所有详细信息
set RUST_BACKTRACE=full && cargo run
2. 9.2用 Result 处理可恢复的错误
代码如下
pub fn ResultS_1() {
let file = File::open("aaa.text");
let f = match file {
Ok(file) => println!("获得了文件"),
Err(err) => {
panic!("读取文件发生错误:{:#?}", err);
}
};
}
pub fn ResultS_2() {
let greeting_file_result = File::open("hello.txt");
let greeting_file = match greeting_file_result {
Ok(file) => file,
Err(error) => match error.kind() {
ErrorKind::NotFound => match File::create("hello.txt") {
Ok(fc) => fc,
Err(e) => panic!("Problem creating the file: {:?}", e),
},
other_error => {
panic!("Problem opening the file: {:?}", other_error);
}
},
};
}
pub fn ResultS_3() {
let greeting_file = 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);
})
} else {
panic!("Problem opening the file: {:?}", error);
}
});
}
pub fn ResultS_4() {
let greeting_file = File::open("hello.txt").unwrap();
}
pub fn ResultS_5() {
let greeting_file =
File::open("hello.txt").expect("文件读取错误");
}
- 创建自定义类型结构体,并进行验证
pub struct Guess {
value: i32,
}
impl Guess {
pub fn new(value: i32) {
if value < 1 || value > 100 {
panic!("值错误必须大于1 小于100 你输入的值,{}",value);
Guess{value};
}
}
pub fn value(&self)->i32{
self.value
}
}
pub mod Trait_s {
pub trait Summary {
fn summarize(&self) -> String{
println!("默认实现");
String::from("默认实现")
}
}
pub struct Twitter {
username: String,
headline: String,
}
impl Summary for Twitter {
fn summarize(&self) -> String {
println!("类型推特 ,用户名{},标题{}",self.username,self.headline);
format!("类型推特 ,用户名{},标题{}",self.username,self.headline)
}
}
pub struct Microblog {
author: String,
headline: String,
}
impl Summary for Microblog {
/* fn summarize(&self) -> String {
println!("类型微博 ,作者{},标题{}",self.author,self.headline);
format!( "类型微博 ,作者{},标题{}",self.author,self.headline)
} */
} }
pub trait Summary {
fn summarize(&self) -> String {
println!("默认实现");
String::from("默认实现")
}
}
pub struct Twitter {
username: String,
headline: String,
}
impl Summary for Twitter {
fn summarize(&self) -> String {
println!("类型推特 ,用户名{},标题{}", self.username, self.headline);
format!("类型推特 ,用户名{},标题{}", self.username, self.headline)
}
}
pub struct Microblog {
author: String,
headline: String,
}
impl Summary for Microblog {
/* fn summarize(&self) -> String {
println!("类型微博 ,作者{},标题{}",self.author,self.headline);
format!( "类型微博 ,作者{},标题{}",self.author,self.headline)
} */
}
pub fn tra02_a1(ietm1: impl Summary, ietm2: impl Summary) {}
pub fn tra02_a2<T: Summary>(ietm1: T, ietm2: T) {}
use std::fmt::{Debug, Display, format};
pub fn tra02_b1(ietm1: impl Summary + Display) {}
pub fn tra02_b2<T: Summary + Display>(ietm1: T, ietm2: T) {}
pub fn tra02_c<T, U>(a: T, b: U) -> String
where
T: Summary + Display,
U: Summary + Debug,
{
format!("aaaaaaaaaaaa")
}