Rust笔记

  1. 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("文件读取错误");
    }
  1. 创建自定义类型结构体,并进行验证
    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")
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值