Rust类型学习

起因

这一段时间,对新兴的Rust语言有了兴趣,先对类型有了认识,后面学习分支,迭代等。

rust是一门新兴的语言,内存安全,且无垃圾回收机制,可以说是系统级编程语言。Rust语法有部分和Go相识,但有更多的不同,学习曲线相对于C++还是要简单些的。里面有不少特性和机制是其他语言没有的。

先了解一下类型

rust类型,分为基本类型和复合类型

基本类型

fn main(){
    //基本类型
    let flag: bool = true;      //bool只有两个值true和false

    //无符号整型
    let x1: u8 = 56;            //u8类型,占用1个字节
    let x2: u16 = 110;          //u16类型,占用2个字节
    let x3: u32 = 1000;         //u32类型,占用4个字节
    let x4: u64 = 10000000;     //u64类型,占用8个字节
    let x5: u128 = 100000000;   //u128类型,占用16个字节

    //符号整数
    let x6: i8 = -56;             //i8类型,占用1个字节
    let x7: i16 = 110;            //i16类型,占用2个字节
    let x8: i32 = 1000;           //i32类型,占用4个字节
    let x9: i64 = 10000000;       //i64类型,占用8个字节
    let x10: i128 = 100000000;    //i128类型,占用16个字节

    //浮点数类型分f32和f64
    let f1:f32= 3223.22;   //单精度位浮点数
    let f2:f64 = 10.0;     //双精度浮点数
    let f3 =10.0;          //f3变量,类型推导为f64双精度浮点数
}

字符类型

let c1 = 'a';     //定义字符类型
let c2 = '\'';    //字符转义其他语言一样,用\,两个\\输出一个\, 单引号出输出\'
println!("{}",c1);
println!("{}",c2);

复合类型,又分为数组和元组

//数组
let arr1: [i32; 3] = [0, 1, 2];      //声明一个长度为3的i32类型数组,这个用法稍微有点怪
let arr2 = [0, 1, 2];                //一般都使用简写

let last_val = arr1[2];              //访问数组下标为2的值
println!("array arr[2]={}", last_val);

//元组,在rust中是原生的类型,和csharp(c#)不一样
let xx = (12, "hello rust", 110.1);  //声明一个类型有i32类型,字符串类型,f64类型的元组
//使用下标获取元组的元素
println!("index=0 val={}", xx.0);
println!("index=1 val={}", xx.1);
println!("index=2 val={}", xx.2);

//使用多个变量按照顺序接受元组的元素值
let (a, b, c) = xx;
println!("a={}", a);
println!("b={}", b);
println!("c={}", c);

范围类型

let x = std::ops::Range { start: 1, end: 10 };  //声明一个Range左闭右开区间范围类型
let y = (1..10);                                //左闭右开区间简写的方式
assert_eq!(x, y);


let x2 = std::ops::RangeInclusive::new(1, 10);  //全闭区间范围类型
let y2 = (1..=10);                              //全闭区间简写方式
assert_eq!(x2, y2);

这里开始使用断言,看两个集合是否相同

切片类型

let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];

//切片类型 通过引用符&对数组引用,产生切片,进行范围操作
let lists = &arr[0..5];               //切片类型通过数组地址获取数组下标为0开始,5个元素,中间不产生新的数组

assert_eq!(lists.len(), 5);           //验证lists的长度是否为5

//遍历lists的元素,和arr前五个元素进行对比
for item in lists {
    print!("{} ", item);
}
println!();

结构体

///# 结构体
///# 定义结构体,通过#[derive(Debug)]注解,才能让println!打印结构体内容
#[derive(Debug)]
struct People {
    id:u32,            //id
    name:&'static str, //姓名 字符串
    sex:&'static str,  //性别
}
///# 输出结构体内容
fn struct_test(){
   let p =  People{
        id:1,
        name:"tom",
        sex:"男"
    };

    //如果在结构体定义的时候不加上#[derive(Debug)]注解,无法通过println!宏直接打印结构体内容
    //error[E0277]: `People` doesn't implement `std::fmt::Debug`
    //add `#[derive(Debug)]` or manually implement `std::fmt::Debug`
    //u8 bool等类型可以通过println!打印,是因为这些类型默认实现Display,这个后面在说
    //下边没有{}而是改为{:?}输出结构体内容,{:#?}带缩进的输出
    println!("people = {:?}",p);
}

枚举

#[derive(Debug)]
enum  WindowState{
    Min,       //最小化
    Normal,    //正常
    Max,       //最大化
}
fn enum_test(){
    let status = WindowState::Max;
    match status {
        WindowState::Normal =>println!("normal"),
        WindowState::Min=>println!("min"),
        WindowState::Max=>println!("max"),
    }
    //通过将枚举status转为u8类型,发现这点和其他语言一样
    println!("enum min val={}",WindowState::Min as u8);
    println!("enum normal val={}",WindowState::Normal as u8);
    println!("enum max val={}",WindowState::Max as u8);
}


秋风 2019-09-27