RUST学习1

运行命令

cargo doc --open
rustup doc
cargo new --vcs=git
cargo build
cargo run
cargo check 更快编译,但不生成可执行文件
cargo build --release 生成的执行文件运行更快
benchmark基准测试

Variables and Mutability
mutable 易变的

let mut x=5

rust默认变量不可变

fn main() {
const MAX_POINTS: u32 = 100_000;
}
const 整个运行时间都存在

fn main() {
let spaces = “ “;
let spaces = spaces.len();
}

Data Types

interger 默认i32
float 默认 f64
char ‘’ 4字节*Accented letters; Chinese, Japanese, and Korean characters; emoji; and zero-width spaces are all valid char values in Rust. *
string “”

tuple 元组 固定 类型可以不同

1
2
3
4
5
6
7
8
9
10
11
12
fn main() {
let tup: (i32, f64, u8) = (500, 6.4, 1);
}
fn main() {
let x: (i32, f64, u8) = (500, 6.4, 1);

let five_hundred = x.0;

let six_point_four = x.1;

let one = x.2;
}

array 数组 类型相同 固定 存在栈中

1
2
3
4
fn main() {
let a = [1, 2, 3, 4, 5];
}

1
2
3
4
fn main() {
let months = ["January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December"];
}
1
2
3
4
5
6
7
8
9
fn main() {
let a: [i32; 5] = [1, 2, 3, 4, 5];
}


fn main() {
let a = [3; 5];
}
let first = a[0];

rust会检查索引跃出

vector 不固定

Functions

1
2
3
4
5
6
7
8
9
fn main() {
println!("Hello, world!");

another_function();
}

fn another_function() {
println!("Another function.");
}

Rustdoesn’t carewhere you define your functions, only that they’re defined somewhere.

1
2
3
4
5
6
7
8
9
fn main() {
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);
}

必须声明参数类型


Statementsare instructions that perform some action and do not return a value.
let y = 6; 无返回值不能赋给其他变量
Expressions evaluate to a resulting value.

1
2
3
4
5
6
7
8
9
10
fn main() {
let x = 5;

let y = {
let x = 3;
x + 1
};

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

Note the x + 1 line without a semicolon at the end, which is unlike most of the lines you’ve seen so far. Expressions do not include ending semicolons. If you add a semicolon to the end of an expression, you turn it into a statement, which will then not return a value.

返回值

1
2
3
4
5
6
7
8
9
fn five() -> i32 {
5
}

fn main() {
let x = five();

println!("The value of x is: {}", x);
}
1
2
3
4
5
6
7
8
9
10
fn main() {
let x = plus_one(5);

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

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

Comments

//

control

1
2
3
4
5
6
7
8
9
10
fn main() {
let number = 3;

if number < 5 {
println!("condition was true");
} else {
println!("condition was false");
}
}

Rust will not automatically try to convert non-Boolean types to a Boolean

1
2
3
4
5
6
7
8
9
10
11
12
13
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");
}
}
1
2
3
4
5
6
7
fn main() {
let condition = true;
let number = if condition { 5 } else { 6 };

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

循环
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
fn main() {
loop {
println!("again!");
}
}

fn main() {
let mut counter = 0;

let result = loop {
counter += 1;

if counter == 10 {
break counter * 2;
}
};

println!("The result is {}", result);
}
while
1
2
3
4
5
6
7
8
9
10
11
12
fn main() {
let mut number = 3;

while number != 0 {
println!("{}!", number);

number -= 1;
}

println!("LIFTOFF!!!");
}

for

use a for loop and execute some code for each item in a collection

1
2
3
4
5
6
7
fn main() {
let a = [10, 20, 30, 40, 50];

for element in a.iter() {
println!("the value is: {}", element);
}
}
1
2
3
4
5
6
fn main() {
for number in (1..4).rev() {
println!("{}!", number);
}
println!("LIFTOFF!!!");
}

rev, to reverse the range

guess game . example

1
2
3
4
5
6
7
fn main() {
let x = 5;
let y = 10;

println!("x = {} and y = {}", x, y);
}

cargo doc --open

The** trim **method on a String instance will eliminate any whitespace at the beginning and end
5\n 变成 5
The **parse **method on strings parses a string into some kind of number.

cargo fmt 自动调整格式
https://github.com/rust-lang/rustfmt