RUST learning diary Lesson 17 - process control
0x00 review and opening
The knowledge of Rust data types is over for the time being. Start from this section to explain the process control in Rust. There are two ways to control the code execution process in Rust -- conditional judgment and loop.
0x01 condition judgment
The if expression is common in any language, and its form is as follows:
if condition1 {
block1
} else if condition2 {
block2
} else {
block3
}
All conditions are bool expressions. Unlike C language, Rust does not have a non-0 value indicating true and a 0 value indicating false. Curly braces for condition are not required.
Let's practice with a question: find the week corresponding to a number. Take the remainder of a number pair of 7. If it is 0, Sunday is selected, and so on. If it is 6, week 6 is selected.
The example code is as follows:
// Find the week corresponding to a number. Take the remainder of a number pair of 7. If it is 0, Sunday is selected, and so on. If it is 6, week 6 is selected let week: u32 = 1; if week % 7 == 0 { println!("You chose Sunday!") } else if week % 7 == 1 { println!("You chose Monday!") } else if week % 7 == 2 { println!("You chose Tuesday!") } else if week % 7 == 3 { println!("You chose Wednesday!") } else if week % 7 == 4 { println!("You chose Thursday!") } else if week % 7 == 5 { println!("You chose Friday!") } else { println!("You chose Saturday!") }
The running results of the code are as follows:
You chose Monday!
0x02 cycle
Loop, as the name suggests, means to repeatedly execute a piece of code. In Rust, there are three loop forms: loop, while, for in.. Cycle. There are two statements that control loops: break and continue.
break and continue
Both break and continue are used for loop control. Break is used to exit the loop directly. Continue means to jump out of the loop of the current round and not execute the code after continue, but it will still execute adjustment judgment again to decide whether to execute the next loop.
Loop loop
Repeated execution will never end. If there is no exit condition, it is an endless loop. It's easy to use.
Its signature form is:
loop {
//Code to execute the loop
}
The example code is as follows:
let mut count = 0; loop { count += 1; // A counter of 5 jumps out of the loop if count == 5 { break; } } println!("count = {}", count);
Code run result:
loop loop -> count = 5
while Loop
while is followed by an expression. If the value of the expression is true, the loop will be executed.
The example code is as follows:
let mut count = 0; while count < 5 { count += 1; } println!("while loop -> count = {}", count);
Code run result:
while loop -> count = 5
for..in.. Circulation######
Repeat the specified number of times.
The example code is as follows:
let mut count = 0; for i in 1..=5 { count = i } println!("for..in..loop -> count = {}", count);
Code run result:
for..in..loop -> count = 5
The previous example code uses break as an example. Next, practice the use of continue. What is the final result of count after the following code is run?
The example code is as follows:
let mut count = 1; while count < 5 { if count % 2 == 0 { count += 5; continue; } count += 1; }
Code run result:
continue test -> count = 7
Explanation:
At the beginning of the code, count is assigned to 1, and then a while loop is passed. When count < 5, the loop is executed.
In the loop body, if count% 2 = = 0, execute the condition code block. Of course, the first cycle count == 1, obviously count% 2= 0 does not meet the conditions. Continue to execute count += 1 to get count == 2.
For the second cycle, count == 2, meet count% 2 = = 0, execute count += 5 to get count == 7, and then continue. Instead of executing the following code in the loop body, execute the third cycle.
For the third cycle, count == 7. If the condition of count < 5 is not met, exit the cycle and the output value of count is 7.
0x03 match pattern matching
match
In Rust, there is no switch statement in other languages. Instead, match pattern matching is used. Match is used to determine whether the current value matches one of a series of patterns. Patterns can consist of literal values, variables, and wildcards. Like switch in other languages, each pattern is a branch.
PS: in Rust, match pattern matching requires exhausting all possibilities, otherwise it will lead to program error. Can be added at the end of a series of patterns_ Wildcard, indicating all other modes not specified (equivalent to default in Java).
The sample code is as follows (turn the first example into a match Implementation):
// Find the week corresponding to a number. Take the remainder of a number pair of 7. If it is 0, Sunday is selected, and so on. If it is 6, week 6 is selected let week: u32 = 6; match week % 7 { 0 => { println!("You chose Sunday!") } 1 => { println!("You chose Monday!") } 2 => { println!("You chose Tuesday!") } 3 => { println!("You chose Wednesday!") } 4 => { println!("You chose Thursday!") } 5 => { println!("You chose Friday!") } 6 => { println!("You chose Saturday!") } _ => { println!("unknown!") } }
Code run result:
You chose Saturday!
if let and while let
It may seem cumbersome to use match in some situations. Therefore, if let and while let are also provided in Rust to replace match in some scenarios.
if let
The form of if let is as follows:
if let pattern = expr {
block1
} else {
block2
}
When using if let for matching, it should be noted that pattern and expr are single = signs directly. Let's take the example at the beginning of this chapter to explain. Suppose I just want to match the evil spirit of Friday without any other requirements, we can use if let to transform it.
The example code is as follows:
let week: u32 = 5; if let 5 = week % 7 { println!("You chose Friday!"); } else { println!("unknown!"); }
Code run result:
You chose Friday!
while let
The form of while let is as follows:
while let pattern = expr {
block
**}
Now there is a requirement to output the values in the vector in reverse order.
Implementation using loop and match:
let mut vec = vec![1, 3, 5, 7, 9]; loop { match vec.pop() { None => { break; } Some(value) => { print!("{} ", value); } } }
Transform to while let:
let mut vec = vec![1, 3, 5, 7, 9]; while let Some(value) = vec.pop() { print!("{} ", value); }
Code run result:
match loop: 9 7 5 3 1 while let: 9 7 5 3 1
Obviously, the code modified by while let is relatively concise. It actually feels like a grammar candy given to us by Rust.
0x04 summary
This section introduces process control, loop and match matching. Finally, it is introduced that using while let and if let can simplify the code in some scenarios. In comparison, this class is relatively simple and easy.
0x05 source code of this section
017. StudyRust - Code cloud - Open Source China (gitee.com)
Next section Preview - functions.