rust的类型 系统 - 前端笔记-fn main() { println!(111); //① 泛型 及一套代码可以应用多种类型 &...

学习笔记

点滴记忆
回忆过往
首页>> web后端 >>rust的类型 系统 - 前端笔记
fn main() {
    println!("111");
    //① 泛型   及一套代码可以应用多种类型
    let mut vec_int: Vec<i32> = vec![1, 2, 3, 4, 5];
    vec_int.push(10);
    // vec_int.push("大大")// 报错 push的不是一个int
    println!("{:?}", vec_int);

    // 泛型结构体
    struct Apart<T> {
        width: T,
        height: T,
    }
    struct Aparts<T, U> {
        width: T,
        height: U,
    }

    impl<T> Apart<T> {
        fn width(&self) -> &T {
            &self.width
        }
        fn height(&self) -> &T {
            &self.height
        }
    }
    impl<T, U> Aparts<T, U> {
        fn width(&self) -> &T {
            &self.width
        }
        fn height(&self) -> &U {
            &self.height
        }
    }

    let rect = Apart {
        width: 10,
        height: 10,
    };
    let option = rect.width();
    println!("{}", option); // 10
    let rect1 = Aparts {
        width: 100,
        height: 20.12,
    };

    let option = rect1.height();
    println!("{}", option); // 20.12

    // option<T> 泛型枚举  Some<T>任意类型  None<T> 简写 None就好
    // is_none() 是判断 是否是none  is_some() 是判断是否是some
    // unwrap() 是取Option<T> 值的   (但是要保证option有值不然会报错)
    fn option_add(x: Option<i32>, y: Option<i32>) -> Option<i32> {
        if x.is_none() && y.is_none() {
            None
        } else if x.is_none() {
            y
        } else if y.is_none() {
            x
        } else {
            Some(x.unwrap() + y.unwrap())
        }
    }

    let result1 = option_add(Some(11), Some(22));
    let result2 = option_add(Some(10), None);
    let result3 = option_add(None, None);

    println!("{:?}", result1);// Some(33)
    println!("{:?}", result2);// Some(10)
    println!("{:?}", result3);// None

    // 泛型函数
    fn foo<T>(x: T) -> T {
        x
    }
    println!("{}", foo(100)); // 100

    // ② trait系统 定义组方法的原型,
    // 抽象方法,没有具体实现的方法 只是定义 (使用了trait系统,必须实现其定义的抽象方法)
    // 具体方法:已经定义好了方法,(会被 trait的结构体重载方法)

    // 定义 trait(以下是两个抽象方法)
    trait Geo {
        fn area(&self) -> f32;
        fn perimeter(&self) -> f32;
    }

    struct Rectangle {
        width: f32,
        height: f32,
    }
    struct Circle {
        radius: f32,
    }
    // 使用 trait
    impl Geo for Rectangle {
        fn area(&self) -> f32 {
            self.width * self.height
        }
        fn perimeter(&self) -> f32 {
            (self.width + self.height) * 2.0
        }
    }
    // 使用 trait
    impl Geo for Circle {
        fn area(&self) -> f32 {
            self.radius * 3.14 * self.radius
        }
        fn perimeter(&self) -> f32 {
            self.radius * 3.14 * 2.0
        }
    }
    let aect1 = Rectangle { width: 5.0, height: 10.0 };
    let aect2 = Circle { radius: 9.0 };
    println!("{}--{}", aect1.area(), aect1.perimeter());// 50--30
    println!("{}--{}", aect2.area(), aect2.perimeter());// 254.34--56.52

    // ③ 类型转换

    // 数字类型转换
    let x: u16 = 7;
    let x1 = std::u32::MAX;
    let x2 = 65u8;
    let x3 = 'A';
    let x4 = 7.7;

    let result = x as i32;
    println!("u16 转 i32={}", result);// u16 转 i32=7
    let result = x1 as i64;
    println!("u32 转 i64={}", result);// u32 转 i64=4294967295
    let result = x2 as i16;
    println!("u8 转 i16={}", result);// u8 转 i16=65
    let result = x3 as i8;
    println!("char 转 i8={}", result);// char 转 i8=65
    let result = x4 as f32;
    println!("f64 转 f32={}", result);// f64 转 f32=7.7

    // 数字和String转换  to_string转成 字符串   parse转成int类型
    let y = x.to_string();
    let y1 = x4.to_string();
    println!("{}--{}", y, y1); // 7--7.7

    let string_value1 = String::from("7.1");
    let string_value2 = String::from("1");
    let string_value3 = String::from("站事干");

    let y = string_value1.parse::<f32>().unwrap();
    let back_int = string_value2.parse::<i32>().unwrap();
    let y1 = string_value2.parse::<i8>().unwrap();
    let err = string_value3.parse::<i32>();
    match err {
        Ok(e) => {
            println!("成功--{}", e);
        }
        Err(e) => {
            println!("失败--{}", e)
        }
    }
    println!("{}---{}--{}", back_int, y1,y);

    // &str 和String转换

    let x = String::from("hello");
    let y = x.as_str();
    println!("{}", y);
    let x = "hello";
    let y = x.to_string();
    println!("{}", y);
}

×

感谢您的支持,我们会一直保持!

扫码支持
请土豪扫码随意打赏

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

打赏作者
版权所有,转载注意明处:前端笔记 » rust的类型 系统

发表评论

路人甲 表情
Ctrl+Enter快速提交

网友评论(0)