add solutions for flow-control.md
This commit is contained in:
parent
0f6c587ce7
commit
8019f09714
|
@ -0,0 +1,213 @@
|
|||
1.
|
||||
```rust
|
||||
fn main() {
|
||||
let n = 5;
|
||||
|
||||
if n < 0 {
|
||||
println!("{} is negative", n);
|
||||
} else if n > 0 {
|
||||
println!("{} is positive", n);
|
||||
} else {
|
||||
println!("{} is zero", n);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
2.
|
||||
```rust
|
||||
fn main() {
|
||||
let n = 5;
|
||||
|
||||
let big_n =
|
||||
if n < 10 && n > -10 {
|
||||
println!(", and is a small number, increase ten-fold");
|
||||
|
||||
10 * n
|
||||
} else {
|
||||
println!(", and is a big number, halve the number");
|
||||
|
||||
n / 2
|
||||
};
|
||||
|
||||
println!("{} -> {}", n, big_n);
|
||||
}
|
||||
```
|
||||
|
||||
3.
|
||||
```rust
|
||||
fn main() {
|
||||
for n in 1..100 {
|
||||
if n == 100 {
|
||||
panic!("NEVER LET THIS RUN")
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
4.
|
||||
```rust
|
||||
fn main() {
|
||||
let names = [String::from("liming"),String::from("hanmeimei")];
|
||||
for name in &names {
|
||||
// do something with name...
|
||||
}
|
||||
|
||||
println!("{:?}", names);
|
||||
|
||||
let numbers = [1, 2, 3];
|
||||
// the elements in numbers are Copy,so there is no move here
|
||||
for n in numbers {
|
||||
// do something with name...
|
||||
}
|
||||
|
||||
println!("{:?}", numbers);
|
||||
}
|
||||
```
|
||||
|
||||
5.
|
||||
```rust
|
||||
fn main() {
|
||||
let a = [4, 3, 2, 1];
|
||||
|
||||
// iterate the indexing and value in 'a'
|
||||
for (i,v) in a.iter().enumerate() {
|
||||
println!("The {}th element is {}",i+1,v);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
6.
|
||||
```rust
|
||||
fn main() {
|
||||
// A counter variable
|
||||
let mut n = 1;
|
||||
|
||||
// Loop while the condition is true
|
||||
while n < 10 {
|
||||
if n % 15 == 0 {
|
||||
println!("fizzbuzz");
|
||||
} else if n % 3 == 0 {
|
||||
println!("fizz");
|
||||
} else if n % 5 == 0 {
|
||||
println!("buzz");
|
||||
} else {
|
||||
println!("{}", n);
|
||||
}
|
||||
|
||||
|
||||
n += 1;
|
||||
}
|
||||
|
||||
println!("n reached {}, soloop is over",n);
|
||||
}
|
||||
```
|
||||
|
||||
7.
|
||||
```rust
|
||||
fn main() {
|
||||
let mut n = 0;
|
||||
for i in 0..=100 {
|
||||
if n == 66 {
|
||||
break
|
||||
}
|
||||
n += 1;
|
||||
}
|
||||
|
||||
assert_eq!(n, 66);
|
||||
}
|
||||
```
|
||||
|
||||
8.
|
||||
```rust
|
||||
fn main() {
|
||||
let mut n = 0;
|
||||
for i in 0..=100 {
|
||||
if n != 66 {
|
||||
n+=1;
|
||||
continue;
|
||||
}
|
||||
|
||||
break
|
||||
}
|
||||
|
||||
assert_eq!(n, 66);
|
||||
}
|
||||
```
|
||||
|
||||
9.
|
||||
```rust
|
||||
fn main() {
|
||||
let mut count = 0u32;
|
||||
|
||||
println!("Let's count until infinity!");
|
||||
|
||||
// Infinite loop
|
||||
loop {
|
||||
count += 1;
|
||||
|
||||
if count == 3 {
|
||||
println!("three");
|
||||
|
||||
// Skip the rest of this iteration
|
||||
continue;
|
||||
}
|
||||
|
||||
println!("{}", count);
|
||||
|
||||
if count == 5 {
|
||||
println!("OK, that's enough");
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
assert_eq!(count, 5);
|
||||
}
|
||||
```
|
||||
|
||||
10.
|
||||
```rust
|
||||
fn main() {
|
||||
let mut counter = 0;
|
||||
|
||||
let result = loop {
|
||||
counter += 1;
|
||||
|
||||
if counter == 10 {
|
||||
break counter * 2;
|
||||
}
|
||||
};
|
||||
|
||||
assert_eq!(result, 20);
|
||||
}
|
||||
```
|
||||
|
||||
11.
|
||||
```rust
|
||||
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
|
||||
}
|
||||
count += 2;
|
||||
}
|
||||
|
||||
count += 5;
|
||||
|
||||
'inner2: loop {
|
||||
if count >= 30 {
|
||||
// This breaks the outer loop
|
||||
break 'outer;
|
||||
}
|
||||
|
||||
// This will continue the outer loop
|
||||
continue 'outer;
|
||||
}
|
||||
}
|
||||
|
||||
assert!(count == 30)
|
||||
}
|
||||
```
|
|
@ -1,7 +1,7 @@
|
|||
# Flow control
|
||||
|
||||
### if/else
|
||||
🌟
|
||||
1. 🌟
|
||||
```rust,editable
|
||||
|
||||
// fill in the blanks
|
||||
|
@ -18,7 +18,7 @@ fn main() {
|
|||
}
|
||||
```
|
||||
|
||||
🌟🌟 `if/else` expression can be used in assignments.
|
||||
2. 🌟🌟 `if/else` expression can be used in assignments.
|
||||
```rust,editable
|
||||
|
||||
// fix the errors
|
||||
|
@ -41,7 +41,7 @@ fn main() {
|
|||
```
|
||||
|
||||
### for
|
||||
🌟 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
|
||||
|
||||
|
@ -55,7 +55,7 @@ fn main() {
|
|||
```
|
||||
|
||||
|
||||
🌟🌟
|
||||
4. 🌟🌟
|
||||
```rust,editable
|
||||
|
||||
// fix the errors without adding or removing lines
|
||||
|
@ -77,7 +77,7 @@ fn main() {
|
|||
}
|
||||
```
|
||||
|
||||
🌟
|
||||
5. 🌟
|
||||
```rust,editable
|
||||
fn main() {
|
||||
let a = [4, 3, 2, 1];
|
||||
|
@ -90,7 +90,7 @@ fn main() {
|
|||
```
|
||||
|
||||
### while
|
||||
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
|
||||
|
||||
|
@ -120,7 +120,7 @@ fn main() {
|
|||
```
|
||||
|
||||
### continue and break
|
||||
🌟 use `break` to break the loop.
|
||||
7. 🌟 use `break` to break the loop.
|
||||
```rust,editable
|
||||
|
||||
// fill in the blank
|
||||
|
@ -137,7 +137,7 @@ fn main() {
|
|||
}
|
||||
```
|
||||
|
||||
🌟🌟 `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
|
||||
|
||||
// fill in the blanks
|
||||
|
@ -158,7 +158,7 @@ fn main() {
|
|||
|
||||
### loop
|
||||
|
||||
🌟🌟 loop is usually used together with `break` or `continue`.
|
||||
9. 🌟🌟 loop is usually used together with `break` or `continue`.
|
||||
|
||||
```rust,editable
|
||||
|
||||
|
@ -192,7 +192,7 @@ fn main() {
|
|||
}
|
||||
```
|
||||
|
||||
🌟🌟 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
|
||||
|
@ -211,7 +211,7 @@ fn main() {
|
|||
}
|
||||
```
|
||||
|
||||
🌟🌟🌟 It's possible to break or continue outer loops when dealing with nested loops. In these cases, the loops must be annotated with some 'label, and the label must be passed to the break/continue statement.
|
||||
11. 🌟🌟🌟 It's possible to break or continue outer loops when dealing with nested loops. In these cases, the loops must be annotated with some 'label, and the label must be passed to the break/continue statement.
|
||||
|
||||
```rust,editable
|
||||
|
||||
|
|
|
@ -90,7 +90,7 @@ fn main() {
|
|||
```
|
||||
|
||||
### while
|
||||
当条件为 true 时,`while` 将一直循环
|
||||
🌟🌟 当条件为 true 时,`while` 将一直循环
|
||||
|
||||
```rust,editable
|
||||
|
||||
|
|
Loading…
Reference in New Issue