2022-03-01 02:27:23 -06:00
# Flow control
2022-04-25 12:03:31 -05:00
### If/else
2022-03-02 07:48:25 -06:00
1. π
2022-03-01 02:27:23 -06:00
```rust,editable
2022-04-25 12:03:31 -05:00
// Fill in the blanks
2022-03-01 02:27:23 -06:00
fn main() {
let n = 5;
if n < 0 {
println!("{} is negative", n);
} __ n > 0 {
println!("{} is positive", n);
} __ {
println!("{} is zero", n);
}
}
```
2022-04-25 12:03:31 -05:00
2. ππ `If/else` expression can be used in assignments.
2022-03-01 02:27:23 -06:00
```rust,editable
2022-04-25 12:03:31 -05:00
// Fix the errors
2022-03-01 02:27:23 -06:00
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.0 ;
}
println!("{} -> {}", n, big_n);
}
```
2022-04-25 12:03:31 -05:00
### For
2022-03-02 07:48:25 -06:00
3. π The `for in` construct can be used to iterate through an Iterator, e.g a range `a..b` .
2022-03-01 02:27:23 -06:00
```rust,editable
fn main() {
for n in 1..=100 { // modify this line to make the code work
if n == 100 {
panic!("NEVER LET THIS RUN")
}
}
2022-03-05 00:48:43 -06:00
2022-04-25 12:03:31 -05:00
println!("Success!");
2022-03-01 02:27:23 -06:00
}
```
2022-03-02 07:48:25 -06:00
4. ππ
2022-03-01 02:27:23 -06:00
```rust,editable
2022-04-25 12:03:31 -05:00
// Fix the errors without adding or removing lines
2022-03-01 02:27:23 -06:00
fn main() {
let names = [String::from("liming"),String::from("hanmeimei")];
for name in names {
2022-04-25 12:03:31 -05:00
// Do something with name...
2022-03-01 02:27:23 -06:00
}
println!("{:?}", names);
let numbers = [1, 2, 3];
2022-04-25 12:03:31 -05:00
// The elements in numbers are CopyοΌ so there is no move here
2022-03-01 02:27:23 -06:00
for n in numbers {
2022-04-25 12:03:31 -05:00
// Do something with name...
2022-03-01 02:27:23 -06:00
}
println!("{:?}", numbers);
}
```
2022-03-02 07:48:25 -06:00
5. π
2022-03-01 02:27:23 -06:00
```rust,editable
fn main() {
let a = [4, 3, 2, 1];
2022-04-25 12:03:31 -05:00
// Iterate the indexing and value in 'a'
2022-03-01 02:27:23 -06:00
for (i,v) in a.__ {
println!("The {}th element is {}",i+1,v);
}
}
```
2022-04-25 12:03:31 -05:00
### While
2022-03-02 07:48:25 -06:00
6. ππ The `while` keyword can be used to run a loop when a condition is true.
2022-03-01 02:27:23 -06:00
```rust,editable
2022-04-25 12:03:31 -05:00
// Fill in the blanks to make the last println! work !
2022-03-01 02:27:23 -06:00
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);
}
__ ;
}
2022-04-25 12:03:31 -05:00
println!("n reached {}, so loop is over",n);
2022-03-01 02:27:23 -06:00
}
```
2022-04-25 12:03:31 -05:00
### Continue and break
7. π Use `break` to break the loop.
2022-03-01 02:27:23 -06:00
```rust,editable
2022-04-25 12:03:31 -05:00
// Fill in the blank
2022-03-01 02:27:23 -06:00
fn main() {
let mut n = 0;
for i in 0..=100 {
if n == 66 {
__
}
n += 1;
}
assert_eq!(n, 66);
2022-03-05 00:48:43 -06:00
2022-04-25 12:03:31 -05:00
println!("Success!");
2022-03-01 02:27:23 -06:00
}
```
2022-03-02 07:48:25 -06:00
8. ππ `continue` will skip over the remaining code in current iteration and go to the next iteration.
2022-03-01 02:27:23 -06:00
```rust,editable
2022-04-25 12:03:31 -05:00
// Fill in the blanks
2022-03-01 02:27:23 -06:00
fn main() {
let mut n = 0;
for i in 0..=100 {
if n != 66 {
n+=1;
__ ;
}
__
}
assert_eq!(n, 66);
2022-03-05 00:48:43 -06:00
2022-04-25 12:03:31 -05:00
println!("Success!");
2022-03-01 02:27:23 -06:00
}
```
2022-04-25 12:03:31 -05:00
### Loop
2022-03-01 02:27:23 -06:00
2022-04-25 12:03:31 -05:00
9. ππ Loop is usually used together with `break` or `continue` .
2022-03-01 02:27:23 -06:00
```rust,editable
2022-04-25 12:03:31 -05:00
// Fill in the blanks
2022-03-01 02:27:23 -06:00
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
__ ;
}
println!("{}", count);
if count == 5 {
println!("OK, that's enough");
__ ;
}
}
assert_eq!(count, 5);
2022-03-05 00:48:43 -06:00
2022-04-25 12:03:31 -05:00
println!("Success!");
2022-03-01 02:27:23 -06:00
}
```
2022-04-25 12:03:31 -05:00
10. ππ Loop is an expression, so we can use it with `break` to return a value
2022-03-01 02:27:23 -06:00
```rust,editable
2022-04-25 12:03:31 -05:00
// Fill in the blank
2022-03-01 02:27:23 -06:00
fn main() {
let mut counter = 0;
let result = loop {
counter += 1;
if counter == 10 {
__ ;
}
};
assert_eq!(result, 20);
2022-03-05 00:48:43 -06:00
2022-04-25 12:03:31 -05:00
println!("Success!");
2022-03-01 02:27:23 -06:00
}
```
2022-03-02 07:48:25 -06:00
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.
2022-03-01 02:27:23 -06:00
```rust,editable
2022-04-25 12:03:31 -05:00
// Fill in the blank
2022-03-01 02:27:23 -06:00
fn main() {
let mut count = 0;
'outer: loop {
'inner1: loop {
if count >= 20 {
// This would break only the inner1 loop
2022-04-25 12:03:31 -05:00
break 'inner1; // `break` is also works.
2022-03-01 02:27:23 -06:00
}
count += 2;
}
count += 5;
'inner2: loop {
if count >= 30 {
// This breaks the outer loop
break 'outer;
}
// This will continue the outer loop
continue 'outer;
}
}
2022-03-05 00:48:43 -06:00
assert!(count == __ );
2022-04-25 12:03:31 -05:00
println!("Success!");
2022-03-01 02:27:23 -06:00
}
2022-03-01 08:06:38 -06:00
```
> You can find the solutions [here](https://github.com/sunface/rust-by-practice)(under the solutions path), but only use it when you need it