邱 璇洛 (ゝ∀・)

邱 璇洛 (ゝ∀・)

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

The Rust Journey Starting from main.rs

Development Environment: MacOS
Rust v1.66.0
Study Notes
Reference Link: Runoob Tutorial-Rust

Recently, I have been learning Rust and had a strange idea of putting almost all the basic knowledge of Rust into one executable file, so...

2020/12/16 - First update, currently up to the part about enumerations

// Struct
#[derive(Debug)]
// Debug mode can use {:?} to output the entire struct
struct Book {
    title: String,
    author: String,
    nation: String,
    date: u32,
    id: u32,
}

// Struct methods
#[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;
    }

    // Associated function of the struct
    fn create(width:u32, height:u32) -> Rectangle{
        return Rectangle {width, height};
    }
}

// Enumeration
// Not yet learned

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 {
        // End the program
        return;
    } else if y < 0 {
        return;
    } else {
        println!("{}", y);
    }

    // Composite types
    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);

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

    // Loops
    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);

    // Clone
    let yy = y.clone();
    println!("yy :: -> [{}]", yy);
    // Borrowing
    let yy_0 = &yy;
    println!("yy_0 :: -> [{}]", yy_0);

    // Slices
    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);
    // Array slices
    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]);

    // Struct
    let author = String::from("George Orwell");
    let id_32 = Book {
        title: String::from("1984"),
        author, // author : author
        nation: String::from("UK"),
        date: 1949,
        id: 1,
    };
    println!("book {:?}", id_32);

    let id_31 = Book {
        author: String::from("George Orwell"),
        id: 31,
        // Others are the same as the previous one
        ..id_32
    };

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

    // Struct methods
    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));

    // Tuple struct
    struct Color(u8, u8, u8);
    let block = Color(0, 0, 0);
    println!("block :: Color -> ({}, {}, {})", block.0, block.1, block.2);

    // Unit struct (not sure what it's used for)
    struct UnitStruct;

    
}
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.