Rust 學習筆記

CargoRun發表於2020-11-02

Rust 學習筆記
[
variables:變數
mutable:可變的
compile:編譯
unoptimized:未優化
mismatched:搭配不當
scalar:標量
compound:複合型別
parse:從語法上分析
annotation:註解,標註
type annotations needed
infer:推斷,推理
panic:驚慌,恐慌
Wrapping:環繞
]

COMM PROGRAMMING CONCEPTS
{變數、基本型別、函式、註釋和控制流}

KEYWORDS
關鍵字
Appendix A
附錄A

Variables and Mutability
變數與可變性

Rust中的變數預設是不可變的。

Error: cannot assign twice to immutable variable

let x = 5;
let mut y = 6;

mut變數不可以更改資料型別

常量 constant
const MAX_POINTS: u32 = 100_000;

隱藏 shadow

fn main() {
    let x = 5;
    let x = x + 1;
    let x = x * 2;
    let spaces = "    ";
    let spaces = spaces.len(); //changed types
    println!("The value of x is: {}", x); //12
    println!("The length of spaces is: {}", spaces); //4
}

Data Types
資料型別

let guess: u32 = “42 is a number?”.parse().expect(“Not a number!”);

Scalar Types
標量型別

Integer Types
整數型別
Length Signed Unsigned
8-bit i8 u8
16-bit i16 u16
32-bit i32 u32
64-bit i64 u64
arch isize usize

Integer Literals in Rust
整數字面量
Number literals Example
Decimal 98_222
Hex 0xff
Octal 0o77
Binary 0b1111_0000
Byte(u8 only) b’A’

Interger Overflow
整數溢位

Floating-Point Types
浮點數型別

fn main() {
    let x = 2.0; //f64
    let y: f32 = 3.0 //f32
    println!("The x,y is: {},{}", x, y); //2.0,3.0
}

Numberic Operations
數值運算

fn main() {
    //addition
    let sum = 5 + 10;

    //subtration
    let difference = 95.5 - 4.3;

    //multiplication
    let product = 4 * 30;

    //division
    let quotient = 56.7 / 32.2;

    remainder
    let remainder = 43 % 5;

    println!("The sum is: {}", sum);
    println!("The difference is: {}", difference);
    println!("The product is: {}", product);
    println!("The quotient is: {}", quotient);
    println!("The remainder is: {}", remainder);
}

The Boolean Type
布林型別

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

The Character Type
字元型別

fn main() {
    let x = 'A';
    let y = '①';
    let z = '©';
    println!("The character x,y,z is {} {} {}", x, y, z);
}

Compound Types
複合型別

The Tuple Type
元組型別

fn main() {
    let tup: (i32, f64, u8) = (500, 6.4, 1);
    let (x, y, z) = tup;
    println!("The x, y, z is {}, {}, {}", x, y, z);
    let five_handred = tup.0;
    let six_point_four = tup.1;
    let one = tup.2;
    println!("The five_handred equals {}", five_handred);
    println!("The six_point_four equals {}", six_point_four);
    println!("The one equals {}", one);
}

The Array Type
陣列型別

fn main() {
    let x = [1, 2, 3, 4, 5];
    let y: [f64; 5] = [0.618, 11.11, 3.14, 4.05, 5.21];
    let z = [3; 5];

    let index = 2;
    let months = ["January", "February", "March",
     "April", "May", "June", "July", "August",
     "September", "October", "November", "December"];
    let element = months[index];

    println!("The array x is {:?}", x);
    println!("The array y is {:?}", y);
    println!("The array z is {:?}", z);
    println!("The value of element is {}", element);
}

Function
函式(snake case)

fn main() {
    println!("Hello, world!");
    another_function();
}
fn another_function() {
    println!("Another function.");
}

Function Parameters
函式引數

fn main() {
    println!("Hello, world!");
    another_function(5, 6);
}
fn another_function(x: i32, y: i32) {
    println!("The value of x is: {}.", x);
    println!("The value of y is: {}.", y);
    println!("The value of x + y is: {}.", x + y);
}

Statements and Expressions in Function Bodies
函式體中的語句和表示式

fn main() {
    let x = 5;
    let y = {
        let x = 1;
        x + 1
    };
    let z = {
        let x = 1;
        x - 1
    };
    println!("The value of x is {}.", x);
    println!("The value of y is {}.", y);
    println!("The value of z is {}.", z);
}

Functions with Return Values
函式的返回值

fn main() {
    let x = five();
    println!("The value of x is: {}", x);
}
fn five() -> i32 {
    5
}
fn main() {
    let x = plus_one(5);
    println!("The value of x is: {}", x);
}
fn plus_one(x: i32) -> i32 {
    x + 1
}

Comments
註釋

fn main() {
    let lucky_number = 7;
    //I'm feeling lucky today.
    println!("The value of lucky_number is {}.", lucky_number);
}

Control Flow
控制流

if Expressions
if表示式

fn main() {
    let number = 3;
    if number < 5 {
        println!("The condition was true");
    } else {
        println!("The condition was false");
    }
}

Rust will not automatically try to convert non-Boolean types to a Boolean.
Rust不會自動嘗試將非布林型的值轉換為布林型別。

fn main() {
    let number = 3;
    if number != 0 {
        println!("The number was something other than zero");
    }
}

Handling Multiple Conditions with else 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");
    }
}

Using if in a let Statement
在let語句中使用if

fn main() {
    let condition = true;
    let number = if condition {
        5
    } else {
        6
    };
    println!("The value of number is: {}", number);
}

Repetition with Loops
使用迴圈重複執行程式碼

Repeating Code with loop
使用loop重複執行程式碼
Returning Values from Loops
從loop迴圈中返回值

fn main() {
    let mut counter = 0;
    let result = loop {
        counter += 1;
        if counter == 10 {
            break counter * 2;
        }
    };
    println!("The value of counter is {}", counter);
    println!("The value of result is {}", result);
}

Conditional Loops with while
while迴圈

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

Looping Through a Collection with for
使用for來迴圈遍歷集合

fn main() {
    let a = [10, 20, 30, 40, 50];
    for element in a.iter() {
        println!("the value is : {}.", element);
    }
}

nicer

fn main() {
    for number in (1.. 4). rev() {
        println!("{}!", number);
    }
    println!(" LIFTOFF!!!");
}
本作品採用《CC 協議》,轉載必須註明作者和本文連結
艱難困苦,玉汝於成

相關文章