Match, if let

Match

  1. 🌟🌟
2. // Fill the blanks
3. enum Direction {
4. East,
5. West,
6. North,
7. South,
8. }

10. fn main() {
11. let dire = Direction::South;
12. match dire {
13. Direction::East => println!("East"),
14. __  => { // Matching South or North here
15. println!("South or North");
16. },
17. _ => println!(__),
18. };
19. }
  1. 🌟🌟 Match is an expression, so we can use it in assignments.
2. fn main() {
3. let boolean = true;

5. // Fill the blank with a match expression:
6. //
7. // boolean = true => binary = 1
8. // boolean = false =>  binary = 0
9. let binary = __;

11. assert_eq!(binary, 1);

13. println!("Success!");
14. }
  1. 🌟🌟 Using match to get the data an enum variant holds.
2. // Fill in the blanks
3. enum Message {
4. Quit,
5. Move { x: i32, y: i32 },
6. Write(String),
7. ChangeColor(i32, i32, i32),
8. }

10. fn main() {
11. let msgs = [
12. Message::Quit,
13. Message::Move{x:1, y:3},
14. Message::ChangeColor(255,255,0)
15. ];

17. for msg in msgs {
18. show_message(msg)
19. }

21. println!("Success!");
22. }

24. fn show_message(msg: Message) {
25. match msg {
26. __ => { // match  Message::Move
27. assert_eq!(a, 1);
28. assert_eq!(b, 3);
29. },
30. Message::ChangeColor(_, g, b) => {
31. assert_eq!(g, __);
32. assert_eq!(b, __);
33. }
34. __ => println!("no data in these variants")
35. }
36. }

matches!

matches! looks like match, but can do something different.

  1. 🌟🌟
2. fn main() {
3. let alphabets = ['a', 'E', 'Z', '0', 'x', '9' , 'Y'];

5. // Fill the blank with `matches!` to make the code work
6. for ab in alphabets {
7. assert!(__)
8. }

10. println!("Success!");
11. }
  1. 🌟🌟
2. enum MyEnum {
3. Foo,
4. Bar
5. }

7. fn main() {
8. let mut count = 0;

10. let v = vec![MyEnum::Foo,MyEnum::Bar,MyEnum::Foo];
11. for e in v {
12. if e == MyEnum::Foo { // Fix the error by changing only this line
13. count += 1;
14. }
15. }

17. assert_eq!(count, 2);

19. println!("Success!");
20. }

If let

For some cases, when matching enums, match is too heavy. We can use if let instead.

  1. 🌟
2. fn main() {
3. let o = Some(7);

5. // Remove the whole `match` block, using `if let` instead
6. match o {
7. Some(i) => {
8. println!("This is a really long string and `{:?}`", i);

10. println!("Success!");
11. }
12. _ => {}
13. };
14. }
  1. 🌟🌟
2. // Fill in the blank
3. enum Foo {
4. Bar(u8)
5. }

7. fn main() {
8. let a = Foo::Bar(1);

10. __ {
11. println!("foobar holds the value: {}", i);

13. println!("Success!");
14. }
15. }
  1. 🌟🌟
2. enum Foo {
3. Bar,
4. Baz,
5. Qux(u32)
6. }

8. fn main() {
9. let a = Foo::Qux(10);

11. // Remove the codes below, using `match` instead
12. if let Foo::Bar = a {
13. println!("match foo::bar")
14. } else if let Foo::Baz = a {
15. println!("match foo::baz")
16. } else {
17. println!("match others")
18. }
19. }

Shadowing

  1. 🌟🌟
2. // Fix the errors in-place
3. fn main() {
4. let age = Some(30);
5. if let Some(age) = age { // Create a new variable with the same name as previous `age`
6. assert_eq!(age, Some(30));
7. } // The new variable `age` goes out of scope here

9. match age {
10. // Match can also introduce a new shadowed variable
11. Some(age) =>  println!("age is a new variable, it's value is {}",age),
12. _ => ()
13. }
14. }

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