Fixed mistakes, and clarified some messages
This commit is contained in:
parent
cc4f60d93c
commit
dc554f690b
|
@ -1,10 +1,10 @@
|
|||
# Flow control
|
||||
|
||||
### if/else
|
||||
### If/else
|
||||
1. π
|
||||
```rust,editable
|
||||
|
||||
// fill in the blanks
|
||||
// Fill in the blanks
|
||||
fn main() {
|
||||
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
|
||||
|
||||
// fix the errors
|
||||
// Fix the errors
|
||||
fn main() {
|
||||
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`.
|
||||
|
||||
```rust,editable
|
||||
|
@ -52,7 +52,7 @@ fn main() {
|
|||
}
|
||||
}
|
||||
|
||||
println!("Success!")
|
||||
println!("Success!");
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -60,19 +60,19 @@ fn main() {
|
|||
4. ππ
|
||||
```rust,editable
|
||||
|
||||
// fix the errors without adding or removing lines
|
||||
// Fix the errors without adding or removing lines
|
||||
fn main() {
|
||||
let names = [String::from("liming"),String::from("hanmeimei")];
|
||||
for name in names {
|
||||
// do something with name...
|
||||
// Do something with name...
|
||||
}
|
||||
|
||||
println!("{:?}", names);
|
||||
|
||||
let numbers = [1, 2, 3];
|
||||
// the elements in numbers are CopyοΌso there is no move here
|
||||
// The elements in numbers are CopyοΌso there is no move here
|
||||
for n in numbers {
|
||||
// do something with name...
|
||||
// Do something with name...
|
||||
}
|
||||
|
||||
println!("{:?}", numbers);
|
||||
|
@ -84,19 +84,19 @@ fn main() {
|
|||
fn main() {
|
||||
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.__ {
|
||||
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.
|
||||
|
||||
```rust,editable
|
||||
|
||||
// fill in the blanks to make the last println! work !
|
||||
// Fill in the blanks to make the last println! work !
|
||||
fn main() {
|
||||
// A counter variable
|
||||
let mut n = 1;
|
||||
|
@ -117,15 +117,15 @@ fn main() {
|
|||
__;
|
||||
}
|
||||
|
||||
println!("n reached {}, soloop is over",n);
|
||||
println!("n reached {}, so loop is over",n);
|
||||
}
|
||||
```
|
||||
|
||||
### continue and break
|
||||
7. π use `break` to break the loop.
|
||||
### Continue and break
|
||||
7. π Use `break` to break the loop.
|
||||
```rust,editable
|
||||
|
||||
// fill in the blank
|
||||
// Fill in the blank
|
||||
fn main() {
|
||||
let mut n = 0;
|
||||
for i in 0..=100 {
|
||||
|
@ -137,14 +137,14 @@ fn main() {
|
|||
|
||||
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.
|
||||
```rust,editable
|
||||
|
||||
// fill in the blanks
|
||||
// Fill in the blanks
|
||||
fn main() {
|
||||
let mut n = 0;
|
||||
for i in 0..=100 {
|
||||
|
@ -158,17 +158,17 @@ fn main() {
|
|||
|
||||
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
|
||||
|
||||
// fill in the blanks
|
||||
// Fill in the blanks
|
||||
fn main() {
|
||||
let mut count = 0u32;
|
||||
|
||||
|
@ -196,14 +196,14 @@ fn main() {
|
|||
|
||||
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
|
||||
|
||||
// fill in the blank
|
||||
// Fill in the blank
|
||||
fn main() {
|
||||
let mut counter = 0;
|
||||
|
||||
|
@ -217,7 +217,7 @@ fn main() {
|
|||
|
||||
assert_eq!(result, 20);
|
||||
|
||||
println!("Success!")
|
||||
println!("Success!");
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -225,14 +225,14 @@ fn main() {
|
|||
|
||||
```rust,editable
|
||||
|
||||
// fill in the blank
|
||||
// Fill in the blank
|
||||
fn main() {
|
||||
let mut count = 0;
|
||||
'outer: loop {
|
||||
'inner1: loop {
|
||||
if count >= 20 {
|
||||
// This would break only the inner1 loop
|
||||
break 'inner1; // `break` is also ok
|
||||
break 'inner1; // `break` is also works.
|
||||
}
|
||||
count += 2;
|
||||
}
|
||||
|
@ -252,7 +252,7 @@ fn main() {
|
|||
|
||||
assert!(count == __);
|
||||
|
||||
println!("Success!")
|
||||
println!("Success!");
|
||||
}
|
||||
```
|
||||
|
||||
|
|
Loadingβ¦
Reference in New Issue