Process control
The execution or repetition of certain code through conditions is a fundamental part of programming languages. The main structures used in Rust to control the flow of program execution are if expressions and loop expressions.
If expression
JS requires parentheses after the if, whereas Rust is very concise:
let num = 5;
// Conditions do not need parentheses
if num < 10 {
println!("Less than 10");
}
Copy the code
The judgment condition can only be a Boolean value:
if num { // The condition must be a Boolean value
println!("true");
}
Copy the code
Rust uses == and ≠ to determine whether two values are equal or unequal, while JS uses === and ≠= :
// Determine equal to 0
if num == 0 {
println!("0");
}
// If the value is not equal to 0
ifnum ! =0 {
println!("not 0");
}
Copy the code
Handle process branches with else if and else:
if num < 10 {
println!("Less than 10")}else if num < 20 {
println!("Less than 20")}else if num < 30 {
println!("Less than 30")}else {
println!("More than 30")}Copy the code
Assign values to variables using if expressions
Because if is an expression, it has a return value:
let condition = 1;
let foo = if condition >= 1 {
5
} else {
6
};
foo / / 5
Copy the code
The expression in the branch must return the same type, otherwise an error will be reported:
let foo = if condition >= 1 {
5
} else {
'6' // Error: expected an integer, but got char
};
Copy the code
As with the return value rule for functions, the last sentence of an expression cannot be followed by a semicolon. Adding a semicolon will return an empty tuple:
let foo = if condition >= 1 {
5
} else {
6; // Error: expecting an integer, but getting an empty tuple: ()
};
Copy the code
cycle
loop
Execute code repeatedly using loop:
loop {
println!("Infinite loop")}Copy the code
Exit the loop using conditions:
let mut count = 0;
loop {
println!("{}", count);
if count >= 3 {
break;
}
count += 1;
}
/ / 1
/ / 2
/ / 3
Copy the code
The loop loop has a return value:
let mut count = 0;
let result = loop {
if count >= 3 {
break count; // Return the value with break
}
count += 1;
};
println!("{}", result);
/ / 3
Copy the code
while
To loop conditionally with while:
let mut count = 0;
while count <= 3 {
count += 1;
println!("{}", count);
}
/ / 1
/ / 2
/ / 3
/ / 4
Copy the code
Implement while with loop:
let mut count = 0;
loop {
if count <= 3 {
count += 1;
println!("{}", count);
} else {
break; }}/ / 1
/ / 2
/ / 3
/ / 4
Copy the code
for
A for loop is commonly used to iterate over various collections:
let arr = [1.2.3];
// arr.iter() returns an iterator equivalent to arr[symbol.iterator]().
// So the for loop can only iterate over collections that have iterators
for item in arr.iter() {
println!("{}", item)
}
/ / 1
/ / 2
/ / 3
Copy the code
Implement a for loop with a while:
let arr = [1.2.3];
let mut index = 0;
while index < arr.len() {
println!("{}", arr[index]);
index += 1;
}
Copy the code
The for loop also supports traversal intervals, before packets, not after packets:
for num in 1.3 {
println!("{}", num);
}
/ / 1
/ / 2
Copy the code
Intervals are actually a type in Rust that can be reversed:
// Invert 1, 2, 3 to 3, 2, 1 using rev()
for num in (1.3).rev() {
println!("{}", num);
}
/ / 3
/ / 2
Copy the code
We found that rust does not support loops that control Pointers like JS:
for (let i = 0; i <= 6; i++) {
console.log(i)
if (i <= 3) {
i++
}
}
/ / 0
/ / 2
/ / 4
/ / 5
/ / 6
Copy the code
In Rust we need a while loop to do this:
let mut i = 0; / / initial value
while i <= 6 { // Determine the condition
println!("{}", i);
if i <= 3 {
i += 1
}
i += 1; / / accumulated value
}
/ / 0
/ / 2
/ / 4
/ / 5
/ / 6
Copy the code