#Rust
Review
- 2024-03-07 07:55
一、Introduction #
ifloopwhileformatch
if Expressions
#
Blocks of code associated with the conditions in if expressions are sometimes called arms, just like the arms in match expressions
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 {
println!("number is not divisible by 4, 3, or 2");
}
}Using if in a let Statement
#
fn main() {
let condition = true;
let number = if condition { 5 } else { 6 };
println!("The value of number is: {number}");
}Repetition with Loops #
Rust has three kinds of loops: loop, while, and for.
Repeating Code with loop
#
The loop keyword tells Rust to execute a block of code over and over again forever or until you explicitly tell it to stop.
Fortunately, Rust also provides a way to break out of a loop using code. You can place the break keyword within the loop to tell the program when to stop executing the loop.
We also used continue in the guessing game, which in a loop tells the program to skip over any remaining code in this iteration of the loop and go to the next iteration.
fn main() {
loop {
println!("again!");
}
}Returning Values from Loops #
One of the uses of a loop is to retry an operation you know might fail, such as checking whether a thread has completed its job. You might also need to pass the result of that operation out of the loop to the rest of your code. To do this, you can add the value you want returned after the break expression you use to stop the loop; that value will be returned out of the loop so you can use it, as shown here:
fn main() {
let mut counter = 0;
let result = loop {
counter += 1;
if counter == 3 {
println!("continue");
continue;
}
if counter == 5 {
println!("break");
break counter * 2;
}
};
println!("The result is {result}");
}You can also return from inside a loop. While break only exits the current loop, return always exits the current function.
Loop Labels to Disambiguate Between Multiple Loops #
If you have loops within loops, break and continue apply to the innermost loop at that point. You can optionally specify a loop label on a loop that you can then use with break or continue to specify that those keywords apply to the labeled loop instead of the innermost loop. Loop labels must begin with a single quote.
fn main() {
let mut count = 0;
'counting_up: loop {
println!("count = {count}");
let mut remaining = 10;
loop {
println!("remaining = {remaining}");
if remaining == 9 {
break;
}
if count == 2 {
break 'counting_up;
}
remaining -= 1;
}
count += 1;
}
println!("End count = {count}");
}Conditional Loops with while
#
fn main() {
let mut number = 3;
while number != 0 {
println!("{number}!");
number -= 1;
}
println!("LIFTOFF!!!");
}for-in
fn main() {
let a = [10, 20, 30, 40, 50];
for element in a {
println!("the value is: {element}");
}
}for range
fn main() {
for number in (1..4).rev() {
println!("{number}!");
}
println!("LIFTOFF!!!");
}