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 “”
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"); } elseif number % 3 == 0 { println!("number is divisible by 3"); } elseif 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.