Skip to content

Commit c6dad28

Browse files
committed
Auto merge of #224 - nyxtom:feat-enums, r=fmoko
feat: Add enums exercises Creates an exhaustive list of enum exercises. This goes through the basics of different ways to derive enums with mixed data type variants, as well as the use of the all important `match` operator.
2 parents fea8141 + dc15032 commit c6dad28

File tree

5 files changed

+187
-0
lines changed

5 files changed

+187
-0
lines changed

exercises/enums/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
### Enums
2+
3+
Rust allows you to define a type called `enums` which allow you to enumerate possible values. In combination with enums, we have the concept of `pattern matching` in Rust, which makes it easy to run different code for different values of an enumeration. Enums, while available in many languages, Rust's enums are most similar to `algebraic data types` in functional languages, such as F#, OCaml, and Haskell.
4+
5+
#### Book Sections
6+
7+
- [Enums](https://doc.rust-lang.org/book/ch06-00-enums.html)

exercises/enums/enums1.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// enums1.rs
2+
// Make me compile! Scroll down for hints!
3+
4+
#[derive(Debug)]
5+
enum Message {
6+
// TODO: define a few types of messages as used below
7+
}
8+
9+
fn main() {
10+
println!("{:?}", Message::Quit);
11+
println!("{:?}", Message::Echo);
12+
println!("{:?}", Message::Move);
13+
println!("{:?}", Message::ChangeColor);
14+
}
15+
16+
17+
18+
19+
20+
21+
22+
23+
24+
25+
26+
27+
28+
29+
30+
31+
32+
33+
34+
35+
36+
37+
38+
39+
40+
41+
42+
// Hint: The declaration of the enumeration type has not been defined yet.

exercises/enums/enums2.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// enums2.rs
2+
// Make me compile! Scroll down for hints
3+
4+
#[derive(Debug)]
5+
enum Message {
6+
// TODO: define the different variants used below
7+
}
8+
9+
impl Message {
10+
fn call(&self) {
11+
println!("{:?}", &self);
12+
}
13+
}
14+
15+
fn main() {
16+
let messages = [
17+
Message::Move{ x: 10, y: 30 },
18+
Message::Echo(String::from("hello world")),
19+
Message::ChangeColor(200, 255, 255),
20+
Message::Quit
21+
];
22+
23+
for message in &messages {
24+
message.call();
25+
}
26+
}
27+
28+
29+
30+
31+
32+
33+
34+
35+
36+
37+
38+
39+
40+
41+
42+
43+
44+
45+
46+
47+
48+
49+
50+
51+
52+
53+
54+
55+
56+
57+
58+
59+
60+
// Hint: you can create enumerations that have different variants with different types
61+
// such as no data, anonymous structs, a single string, tuples, ...etc

exercises/enums/enums3.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// enums3.rs
2+
// Address all the TODOs to make the tests pass!
3+
4+
enum Message {
5+
// TODO: implement the message variant types based on their usage below
6+
}
7+
8+
struct Point {
9+
x: u8,
10+
y: u8
11+
}
12+
13+
struct State {
14+
color: (u8, u8, u8),
15+
position: Point,
16+
quit: bool
17+
}
18+
19+
impl State {
20+
fn change_color(&mut self, color: (u8, u8, u8)) {
21+
self.color = color;
22+
}
23+
24+
fn quit(&mut self) {
25+
self.quit = true;
26+
}
27+
28+
fn echo(&self, s: String) {
29+
println!("{}", s);
30+
}
31+
32+
fn move_position(&mut self, p: Point) {
33+
self.position = p;
34+
}
35+
36+
fn process(&mut self, message: Message) {
37+
// TODO: create a match expression to process the different message variants
38+
}
39+
}
40+
41+
#[cfg(test)]
42+
mod tests {
43+
use super::*;
44+
45+
#[test]
46+
fn test_match_message_call() {
47+
let mut state = State{
48+
quit: false,
49+
position: Point{ x: 0, y: 0 },
50+
color: (0, 0, 0)
51+
};
52+
state.process(Message::ChangeColor(255, 0, 255));
53+
state.process(Message::Echo(String::from("hello world")));
54+
state.process(Message::Move{ x: 10, y: 15 });
55+
state.process(Message::Quit);
56+
57+
assert_eq!(state.color, (255, 0, 255));
58+
assert_eq!(state.position.x, 10);
59+
assert_eq!(state.position.y, 15);
60+
assert_eq!(state.quit, true);
61+
}
62+
63+
}

info.toml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,20 @@ mode = "test"
8686
path = "exercises/structs/structs2.rs"
8787
mode = "test"
8888

89+
# ENUMS
90+
91+
[[exercises]]
92+
path = "exercises/enums/enums1.rs"
93+
mode = "compile"
94+
95+
[[exercises]]
96+
path = "exercises/enums/enums2.rs"
97+
mode = "compile"
98+
99+
[[exercises]]
100+
path = "exercises/enums/enums3.rs"
101+
mode = "test"
102+
89103
# TESTS
90104

91105
[[exercises]]

0 commit comments

Comments
 (0)