元素熔铸:Rust语言的基石与流转

60 阅读3分钟

常见概念

使用 Cargo 创建项目

打开命令行输入命令,创建项目,编译并运行项目

// 创建项目
$ cargo new hello_cargo
// 构建项目
$ cargo build
// 优化编译项目
$ cargo build --release
// 一步构建并运行项目
$ cargo run
// 该命令快速检查代码确保其可以编译,但并不产生可执行文件
$ cargo check

数据类型

标量(scalar) 类型代表一个单独的值。Rust 有四种基本的标量类型:整型、浮点型、布尔类型和字符类型。

复合类型(Compound types) 可以将多个值组合成一个类型。Rust 有两个原生的复合类型:元组(tuple)和数组(array)。

整型

长度有符号无符号
8-biti8u8
16-biti16u16
32-biti32u32
64-biti64u64
128-biti128u128
archisizeusize

浮点型

带小数点的数字,Rust 的浮点数类型是 f32 和 f64

fn main() {
    let x = 2.0; // f64
    let y: f32 = 3.0; // f32
}

布尔型

Rust 中的布尔类型有两个可能的值:true 和 false。Rust 中的布尔类型使用 bool 表示。

fn main() {
    let t = true;
    let f: bool = false; // with explicit type annotation
}

字符类型

Rust 的 char 类型是语言中最原生的字母类型。

fn main() {
    let c = 'z';
    let z: char = 'ℤ'; // with explicit type annotation
    let heart_eyed_cat = '😻';
}

元组类型

元组是一个将多个其他类型的值组合进一个复合类型的主要方式。元组长度固定:一旦声明,其长度不会增大或缩小。

fn main() {
    let tup: (i32, f64, u8) = (500, 6.4, 1);
    let (_x, y, _z) = tup;
    println!("The value of y is: {y}");
}

数组类型

数组中的每个元素的类型必须相同。

fn main() {
    let a = [1, 2, 3, 4, 5];
    let first = a[0];
    let second = a[1];
    println!("first is {}, second is {}", first, second);
}

变量和可变性

变量

fn main() {
     // 不可变变量定义
    let x = 5;
    println!("The value of x is: {x}");
    // 可变变量定义
    let mut y = 5;
    println!("The value of y is: {y}");
    y = 6;
    println!("The value of y is: {y}");
}

常量

fn main() {
    const THREE_HOURS_IN_SECONDS: u32 = 60 * 60 * 3;
    println!("The value of THREE_HOURS_IN_SECONDS is: {THREE_HOURS_IN_SECONDS}");
}

隐藏

fn main() {
    let x = 5;
    let x = x + 1;
    {
        let x = x * 2;
        println!("The value of x in the inner scope is: {x}");
    }
    println!("The value of x is: {x}");
}

函数

Rust 代码中的函数和变量名使用 snake case 规范风格。在 snake case 中,所有字母都是小写并使用下划线分隔单词。

fn main() {
    let x = plus_one(5);
    println!("The value of x is: {x}");
}

fn plus_one(x: i32) -> i32 {
    x + 1
}

语句和表达式

语句(Statements)是执行一些操作但不返回值的指令。 表达式(Expressions)计算并产生一个值。

fn main() {
    // 大括号中的是表达式
    let y = {
        let x = 3;
        x + 1
    };

    println!("The value of y is: {y}");
}

注释

所有程序员都力求使其代码易于理解,不过有时还需要提供额外的解释。在这种情况下,程序员在源码中留下 注释comments),编译器会忽略它们,不过阅读代码的人可能觉得有用。

// hello, world

控制流

if 表达式,使用 else if 处理多重条件

fn main() {
    let number = 6;

    if number % 4 == 0 {
        println!("number is divisible by 4");
    } else if number % 3 == 0 {
        println!("number is divisible by 3");
    } else if number % 2 == 0 {
        println!("number is divisible by 2");
    } else {
        println!("number is not divisible by 4, 3, or 2");
    }
}
fn main() {
    let condition = true;
    let number = if condition { 5 } else { 6 };
    println!("The value of number is: {number}");
}

使用循环重复执行

使用 loop 重复执行代码

fn main() {
    let mut counter = 0;
    let result = loop {
        counter += 1;
        if counter == 10 {
            break counter * 2;
        }
    };
    println!("The result is {result}");
}
fn main() {
    let mut count = 0;
    'counting_up: loop {
        println!("count = {count}");
        let mut remaining = 10;

        loop {
            println!("remaining = {remaining}");
            if remaining == 9 {
                break;
            }
            if count == 2 {
                break 'counting_up;
            }
            remaining -= 1;
        }

        count += 1;
    }
    println!("End count = {count}");
}

while 条件循环

fn main() {
    let mut number = 3;
    while number != 0 {
        println!("{number}!");
        number -= 1;
    }
    println!("LIFTOFF!!!");
}

使用 for 遍历集合

fn main() {
    let a = [10, 20, 30, 40, 50];

    for element in a {
        println!("the value is: {element}");
    }
}