Flow control

If/else

  1. 🌟
2. // Fill in the blanks
3. fn main() {
4. let n = 5;

6. if n < 0 {
7. println!("{} is negative", n);
8. } __ n > 0 {
9. println!("{} is positive", n);
10. } __ {
11. println!("{} is zero", n);
12. }
13. }
  1. 🌟🌟 If/else expression can be used in assignments.
2. // Fix the errors
3. fn main() {
4. let n = 5;

6. let big_n =
7. if n < 10 && n > -10 {
8. println!(", and is a small number, increase ten-fold");

10. 10 * n
11. } else {
12. println!(", and is a big number, halve the number");

14. n / 2.0 ;
15. }

17. println!("{} -> {}", n, big_n);
18. }

For

  1. 🌟 The for in construct can be used to iterate through an Iterator, e.g a range a..b.
2. fn main() {
3. for n in 1..=100 { // modify this line to make the code work
4. if n == 100 {
5. panic!("NEVER LET THIS RUN")
6. }
7. }

9. println!("Success!");
10. }
  1. 🌟🌟
2. // Fix the errors without adding or removing lines
3. fn main() {
4. let names = [String::from("liming"),String::from("hanmeimei")];
5. for name in names {
6. // Do something with name...
7. }

9. println!("{:?}", names);

11. let numbers = [1, 2, 3];
12. // The elements in numbers are Copy,so there is no move here
13. for n in numbers {
14. // Do something with n...
15. }

17. println!("{:?}", numbers);
18. }
  1. 🌟
1. fn main() {
2. let a = [4, 3, 2, 1];

4. // Iterate the indexing and value in 'a'
5. for (i,v) in a.__ {
6. println!("The {}th element is {}",i+1,v);
7. }
8. }

While

  1. 🌟🌟 The while keyword can be used to run a loop when a condition is true.
2. // Fill in the blanks to make the last println! work !
3. fn main() {
4. // A counter variable
5. let mut n = 1;

7. // Loop while the condition is true
8. while n __ 10 {
9. if n % 15 == 0 {
10. println!("fizzbuzz");
11. } else if n % 3 == 0 {
12. println!("fizz");
13. } else if n % 5 == 0 {
14. println!("buzz");
15. } else {
16. println!("{}", n);
17. }

19. __;
20. }

22. println!("n reached {}, so loop is over",n);
23. }

Continue and break

  1. 🌟 Use break to break the loop.
2. // Fill in the blank
3. fn main() {
4. let mut n = 0;
5. for i in 0..=100 {
6. if n == 66 {
7. __
8. }
9. n += 1;
10. }

12. assert_eq!(n, 66);

14. println!("Success!");
15. }
  1. 🌟🌟 continue will skip over the remaining code in current iteration and go to the next iteration.
2. // Fill in the blanks
3. fn main() {
4. let mut n = 0;
5. for i in 0..=100 {
6. if n != 66 {
7. n+=1;
8. __;
9. }

11. __
12. }

14. assert_eq!(n, 66);

16. println!("Success!");
17. }

Loop

  1. 🌟🌟 Loop is usually used together with break or continue.
2. // Fill in the blanks
3. fn main() {
4. let mut count = 0u32;

6. println!("Let's count until infinity!");

8. // Infinite loop
9. loop {
10. count += 1;

12. if count == 3 {
13. println!("three");

15. // Skip the rest of this iteration
16. __;
17. }

19. println!("{}", count);

21. if count == 5 {
22. println!("OK, that's enough");

24. __;
25. }
26. }

28. assert_eq!(count, 5);

30. println!("Success!");
31. }
  1. 🌟🌟 Loop is an expression, so we can use it with break to return a value
2. // Fill in the blank
3. fn main() {
4. let mut counter = 0;

6. let result = loop {
7. counter += 1;

9. if counter == 10 {
10. __;
11. }
12. };

14. assert_eq!(result, 20);

16. println!("Success!");
17. }
  1. 🌟🌟🌟 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.
2. // Fill in the blank
3. fn main() {
4. let mut count = 0;
5. 'outer: loop {
6. 'inner1: loop {
7. if count >= 20 {
8. // This would break only the inner1 loop
9. break 'inner1; // `break` is also works.
10. }
11. count += 2;
12. }

14. count += 5;

16. 'inner2: loop {
17. if count >= 30 {
18. // This breaks the outer loop
19. break 'outer;
20. }

22. // This will continue the outer loop
23. continue 'outer;
24. }
25. }

27. assert!(count == __);

29. println!("Success!");
30. }

You can find the solutions here(under the solutions path), but only use it when you need it