语句(statement): 是一些执行动作的指令,不会返回值。(statement are instructions that perform some action and do not return value) 表达式(expression): 运行计算一个值。(expression evaluate to a resulting value)
// 单行注释 Rust have another kind comments, document comments, will discuss later.
/// 文档注释 //!
/* 块注释 */
3.5 Control flow流程控制
3.5.1 if语句
1 2 3 4 5 6 7 8
let num = 6; if number % 4 == 0 { println!("number is divisible by 4"); } elseif number % 3 == 0 { println!("number is divisible by 3"); } else { println!("number is not divisible by 4, 3.") }
if is an expression
1
let num = if condition { 5 } else { 6 };
loop
1 2 3 4 5 6 7 8 9
letmut counter = 0;
let result = loop { counter += 1;
if counter == 10 { break counter * 2; } };
while
1 2 3 4 5 6
letmut number = 3; while number != 0 { println!("{}!", number);
number -= 1; }
for
1 2 3 4 5 6 7 8 9
let a = [10, 20, 30, 40, 50];
for element in a.iter() { println!("the value is: {}", element); }
for number in (1..4).rev() { // Range 1..4 println!("{}!", number); }