Enum

  1. 🌟🌟 Enums can be created with explicit discriminator.
2. // Fix the errors
3. enum Number {
4. Zero,
5. One,
6. Two,
7. }

9. enum Number1 {
10. Zero = 0,
11. One,
12. Two,
13. }

15. // C-like enum
16. enum Number2 {
17. Zero = 0.0,
18. One = 1.0,
19. Two = 2.0,
20. }

22. fn main() {
23. // An enum variant can be converted to a integer by `as`
24. assert_eq!(Number::One, Number1::One);
25. assert_eq!(Number1::One, Number2::One);

27. println!("Success!");
28. }
  1. 🌟 Each enum variant can hold its own data.
2. // Fill in the blank
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 msg1 = Message::Move{__}; // Instantiating with x = 1, y = 2
12. let msg2 = Message::Write(__); // Instantiating with "hello, world!"

14. println!("Success!");
15. }
  1. 🌟🌟 We can get the data which an enum variant is holding by pattern match.
2. // Fill in the blank and fix the error
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 msg = Message::Move{x: 1, y: 2};

13. if let Message::Move{__} = msg {
14. assert_eq!(a, b);
15. } else {
16. panic!("NEVER LET THIS RUN!");
17. }

19. println!("Success!");
20. }
  1. 🌟🌟
2. // Fill in the blank and fix the errors
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. }
20. }

22. fn show_message(msg: Message) {
23. println!("{}", msg);
24. }
  1. 🌟🌟 Since there is no null in Rust, we have to use enum Option<T> to deal with the cases when the value is absent.
2. // Fill in the blank to make the `println` work.
3. // Also add some code to prevent the `panic` from running.
4. fn main() {
5. let five = Some(5);
6. let six = plus_one(five);
7. let none = plus_one(None);

9. if let __ = six {
10. println!("{}", n);

12. println!("Success!");
13. }

15. panic!("NEVER LET THIS RUN!");
16. }

18. fn plus_one(x: Option<i32>) -> Option<i32> {
19. match x {
20. __ => None,
21. __ => Some(i + 1),
22. }
23. }
  1. 🌟🌟🌟🌟 Implement a linked-list via enums.
2. use crate::List::*;

4. enum List {
5. // Cons: Tuple struct that wraps an element and a pointer to the next node
6. Cons(u32, Box<List>),
7. // Nil: A node that signifies the end of the linked list
8. Nil,
9. }

11. // Methods can be attached to an enum
12. impl List {
13. // Create an empty list
14. fn new() -> List {
15. // `Nil` has type `List`
16. Nil
17. }

19. // Consume a list, and return the same list with a new element at its front
20. fn prepend(self, elem: u32) -> __ {
21. // `Cons` also has type List
22. Cons(elem, Box::new(self))
23. }

25. // Return the length of the list
26. fn len(&self) -> u32 {
27. // `self` has to be matched, because the behavior of this method
28. // depends on the variant of `self`
29. // `self` has type `&List`, and `*self` has type `List`, matching on a
30. // concrete type `T` is preferred over a match on a reference `&T`
31. // After Rust 2018 you can use self here and tail (with no ref) below as well,
32. // rust will infer &s and ref tail.
33. // See https://doc.rust-lang.org/edition-guide/rust-2018/ownership-and-lifetimes/default-match-bindings.html
34. match *self {
35. // Can't take ownership of the tail, because `self` is borrowed;
36. // Instead take a reference to the tail
37. Cons(_, ref tail) => 1 + tail.len(),
38. // Base Case: An empty list has zero length
39. Nil => 0
40. }
41. }

43. // Return representation of the list as a (heap allocated) string
44. fn stringify(&self) -> String {
45. match *self {
46. Cons(head, __ tail) => {
47. // `format!` is similar to `print!`, but returns a heap
48. // allocated string instead of printing to the console
49. format!("{}, {}", head, tail.__())
50. },
51. Nil => {
52. format!("Nil")
53. },
54. }
55. }
56. }

58. fn main() {
59. // Create an empty linked list
60. let mut list = List::new();

62. // Prepend some elements
63. list = list.prepend(1);
64. list = list.prepend(2);
65. list = list.prepend(3);

67. // Show the final state of the list
68. println!("linked list has length: {}", list.len());
69. println!("{}", list.stringify());
70. }

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