Merge pull request #225 from Tanish-Eagle/editing-flow-control

Fixed mistakes, and clarified some messages
This commit is contained in:
Sunface
2022-04-26 12:54:41 +08:00
committed by GitHub

View File

@ -1,10 +1,10 @@
# Flow control # Flow control
### if/else ### If/else
1. 🌟 1. 🌟
```rust,editable ```rust,editable
// fill in the blanks // Fill in the blanks
fn main() { fn main() {
let n = 5; let n = 5;
@ -18,10 +18,10 @@ fn main() {
} }
``` ```
2. 🌟🌟 `if/else` expression can be used in assignments. 2. 🌟🌟 `If/else` expression can be used in assignments.
```rust,editable ```rust,editable
// fix the errors // Fix the errors
fn main() { fn main() {
let n = 5; let n = 5;
@ -40,7 +40,7 @@ fn main() {
} }
``` ```
### for ### For
3. 🌟 The `for in` construct can be used to iterate through an Iterator, e.g a range `a..b`. 3. 🌟 The `for in` construct can be used to iterate through an Iterator, e.g a range `a..b`.
```rust,editable ```rust,editable
@ -52,7 +52,7 @@ fn main() {
} }
} }
println!("Success!") println!("Success!");
} }
``` ```
@ -60,19 +60,19 @@ fn main() {
4. 🌟🌟 4. 🌟🌟
```rust,editable ```rust,editable
// fix the errors without adding or removing lines // Fix the errors without adding or removing lines
fn main() { fn main() {
let names = [String::from("liming"),String::from("hanmeimei")]; let names = [String::from("liming"),String::from("hanmeimei")];
for name in names { for name in names {
// do something with name... // Do something with name...
} }
println!("{:?}", names); println!("{:?}", names);
let numbers = [1, 2, 3]; let numbers = [1, 2, 3];
// the elements in numbers are Copyso there is no move here // The elements in numbers are Copyso there is no move here
for n in numbers { for n in numbers {
// do something with name... // Do something with name...
} }
println!("{:?}", numbers); println!("{:?}", numbers);
@ -84,19 +84,19 @@ fn main() {
fn main() { fn main() {
let a = [4, 3, 2, 1]; let a = [4, 3, 2, 1];
// iterate the indexing and value in 'a' // Iterate the indexing and value in 'a'
for (i,v) in a.__ { for (i,v) in a.__ {
println!("The {}th element is {}",i+1,v); println!("The {}th element is {}",i+1,v);
} }
} }
``` ```
### while ### While
6. 🌟🌟 The `while` keyword can be used to run a loop when a condition is true. 6. 🌟🌟 The `while` keyword can be used to run a loop when a condition is true.
```rust,editable ```rust,editable
// fill in the blanks to make the last println! work ! // Fill in the blanks to make the last println! work !
fn main() { fn main() {
// A counter variable // A counter variable
let mut n = 1; let mut n = 1;
@ -121,11 +121,11 @@ fn main() {
} }
``` ```
### continue and break ### Continue and break
7. 🌟 use `break` to break the loop. 7. 🌟 Use `break` to break the loop.
```rust,editable ```rust,editable
// fill in the blank // Fill in the blank
fn main() { fn main() {
let mut n = 0; let mut n = 0;
for i in 0..=100 { for i in 0..=100 {
@ -137,14 +137,14 @@ fn main() {
assert_eq!(n, 66); assert_eq!(n, 66);
println!("Success!") println!("Success!");
} }
``` ```
8. 🌟🌟 `continue` will skip over the remaining code in current iteration and go to the next iteration. 8. 🌟🌟 `continue` will skip over the remaining code in current iteration and go to the next iteration.
```rust,editable ```rust,editable
// fill in the blanks // Fill in the blanks
fn main() { fn main() {
let mut n = 0; let mut n = 0;
for i in 0..=100 { for i in 0..=100 {
@ -158,17 +158,17 @@ fn main() {
assert_eq!(n, 66); assert_eq!(n, 66);
println!("Success!") println!("Success!");
} }
``` ```
### loop ### Loop
9. 🌟🌟 loop is usually used together with `break` or `continue`. 9. 🌟🌟 Loop is usually used together with `break` or `continue`.
```rust,editable ```rust,editable
// fill in the blanks // Fill in the blanks
fn main() { fn main() {
let mut count = 0u32; let mut count = 0u32;
@ -196,14 +196,14 @@ fn main() {
assert_eq!(count, 5); assert_eq!(count, 5);
println!("Success!") println!("Success!");
} }
``` ```
10. 🌟🌟 loop is an expression, so we can use it with `break` to return a value 10. 🌟🌟 Loop is an expression, so we can use it with `break` to return a value
```rust,editable ```rust,editable
// fill in the blank // Fill in the blank
fn main() { fn main() {
let mut counter = 0; let mut counter = 0;
@ -217,7 +217,7 @@ fn main() {
assert_eq!(result, 20); assert_eq!(result, 20);
println!("Success!") println!("Success!");
} }
``` ```
@ -225,14 +225,14 @@ fn main() {
```rust,editable ```rust,editable
// fill in the blank // Fill in the blank
fn main() { fn main() {
let mut count = 0; let mut count = 0;
'outer: loop { 'outer: loop {
'inner1: loop { 'inner1: loop {
if count >= 20 { if count >= 20 {
// This would break only the inner1 loop // This would break only the inner1 loop
break 'inner1; // `break` is also ok break 'inner1; // `break` is also works.
} }
count += 2; count += 2;
} }
@ -252,7 +252,7 @@ fn main() {
assert!(count == __); assert!(count == __);
println!("Success!") println!("Success!");
} }
``` ```