-
Notifications
You must be signed in to change notification settings - Fork 29
/
main.rs
66 lines (52 loc) · 1.4 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
fn print_hello() {
println!("Hello, world!");
}
fn print_sum(a: i32, b: i32) {
println!("Sum is: {}", a + b);
}
fn add_two(a: i32) -> i32 {
return a + 2
// 或 `return a + 2;`
}
fn main() {
print_hello();
print_sum(11, 22);
let add_two_result = add_two(33);
println!("Add two result is {}", add_two_result);
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");
}
let condition = true;
let number = if condition { 5 } else { 6 };
println!("The value of number is: {}", number);
loop {
println!("again!");
break; // 无限循环,但这里我们立刻跳出循环
}
let mut number = 3;
while number != 0 {
println!("{}!", number);
number -= 1;
}
println!("LIFTOFF!!!");
let a = [10, 20, 30, 40, 50];
for element in a.iter() {
println!("the value is: {}", element);
}
for number in 1..10 {
if number % 2 == 0 {
continue;
}
println!("Found an odd number: {}", number);
if number == 7 {
break;
}
}
}