邱 璇洛 (ゝ∀・)

邱 璇洛 (ゝ∀・)

你好哇(*゚∀゚*)~这里是邱璇洛的博客,常常用来记录一些技术文章和小日常~(σ゚∀゚)σ
twitter
tg_channel

从main.rs开始的Rust之旅

开发环境 : MacOS
Rust v1.66.0
学习笔记
参考链接 : 菜鸟教程 - Rust

最近在学 Rust,莫名有个奇怪的想法,把 Rust 初级的几乎所有知识全都放到一个可执行文件里,于是.....

2020/12/16 - 第一次更新,目前为止到了枚举的部分

// 结构体
#[derive(Debug)]
// Debug模式可以用{:?}输出整个结构体
struct Book {
    title: String,
    author: String,
    nation: String,
    date: u32,
    id: u32,
}

// 结构体方法
#[derive(Debug)]
struct Rectangle {
    width: u32,
    height: u32
}

impl Rectangle {
    fn area(&self) -> u32 {
        self.width * self.height
    }

    fn wider(&self, rect: &Rectangle) -> bool {
        return self.width > rect.width;
    }

    // 结构体关联函数
    fn create(width:u32, height:u32) -> Rectangle{
        return Rectangle {width, height};
    }
}

// 枚举类
// 还不会

fn add(a:i32, b:i32) -> i32 {
    return a + b;
}

fn main() {
    print!("Hello Rust(1)");
    print!("Hello Rust(2)\n");

    let a: i32 = 100;
    let mut b: f64 = 2.3;
    let str: &str = "Hello World";

    println!("a+b = {}", add(a, b as i32));
    b = 2.4;
    println!("a+b = {}", a as f64 + b);
    println!("{}", str);
    println!("{}", str.len());

    let y: i32 = {
        let x: i32 = 2;
        x + 1
    };
    println!("\ny = 3+1{}", y);
    if y > 10 {
        // 结束程序
        return;
    } else if y < 0 {
        return;
    } else {
        println!("{}", y);
    }

    // 复合类型
    let tup: (i32, f64, &str, u8) = (3, 4.5, "qwq", 3);
    println!(
        "tup0: {}, tup1: {}, tup2: {}, tup3: {}",
        tup.0, tup.1, tup.2, tup.3
    );
    let (x, y, z, n) = tup;
    println!("{} : {} : {} : {}", x, y, z, n);

    // 数组
    let str_a: [&str; 2] = ["awa", "qwq"];
    let i32_a: [i32; 2] = [12, 3];
    let f64_a: [f64; 2] = [1.2, 5.3];

    // 循环
    let mut n = 0;
    while n != 2 {
        println!("str_a[{}]::-> {}", n, str_a[n]);
        n += 1;
    }
    for i in 0..2 {
        println!("i32_a[{}]::-> {}", i, i32_a[i]);
    }
    // or
    for i in i32_a.iter() {
        println!("i32_a[iter] ::-> {}", i);
    }

    n = 0;
    let _f64_1 = loop {
        if n == 1 {
            break f64_a[n];
        }
        n += 1;
    };

    println!("_f64_1 ::-> [{}]", _f64_1);

    // 克隆
    let yy = y.clone();
    println!("yy :: -> [{}]", yy);
    // 租借
    let yy_0 = &yy;
    println!("yy_0 :: -> [{}]", yy_0);

    // 切片
    let s = String::from("broadcast");
    let _s_05: &str = &s[0..5];
    let _s_59: &str = &s[5..9];
    println!("{}={}+{}", s, _s_05, _s_59);
    // 数组切片
    let i32_b: [i32; 5] = [12, 3, 6, 2, 4];
    let i32_b_25 = &i32_b[2..5];
    println!("[{}={}={}]", i32_b_25[0], i32_b_25[1], i32_b_25[2]);

    // 结构体
    let author = String::from("乔治·奥威尔");
    let id_32 = Book {
        title: String::from("1984"),
        author, // author : author
        nation: String::from("英国"),
        date: 1949,
        id: 1,
    };
    println!("book {:?}", id_32);

    let id_31 = Book {
        author: String::from("乔治 奥威尔"),
        id: 31,
        // 其他和别的一样
        ..id_32
    };

    println!("book {:?}", id_31);

    //结构体方法
    let rect = Rectangle{ width: 100, height: 100 };
    let rect_0 = Rectangle{ width: 200, height: 100 };
    let rect_1 = Rectangle::create(300, 20);
    println!("{:?}", rect_1);
    println!("[Area -> {}] :: [Wider -> {}]", rect.area(), rect.wider(&rect_0));

    // 元祖结构体
    struct Color(u8, u8, u8);
    let block = Color(0, 0, 0);
    println!("block :: Color -> ({}, {}, {})", block.0, block.1, block.2);

    // 单元结构体(不知道有啥用)
    struct UnitStruct;

    
}
加载中...
此文章数据所有权由区块链加密技术和智能合约保障仅归创作者所有。