Skip to content

Commit d6d26dd

Browse files
committed
added day 11, fixed edition
1 parent 46fb051 commit d6d26dd

File tree

15 files changed

+180
-10
lines changed

15 files changed

+180
-10
lines changed

Cargo.lock

Lines changed: 15 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,14 @@ Structs
4747

4848
- `ts-node javascript/day-8/src/structs.ts`
4949
- `cargo run -p day-8-structs`
50+
51+
### Day 9
52+
53+
### Day 10
54+
55+
### Day 11
56+
57+
Modules
58+
59+
- `cargo run -p day-11-modules --bin nested-submodules`
60+
- `cargo run -p day-11-traffic-light`

crates/day-10/traits/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "day-10-traits"
33
version = "0.0.0"
4-
edition = "2018"
4+
edition = "2021"
55

66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
77

crates/day-11/modules/Cargo.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[package]
2+
name = "day-11-modules"
3+
version = "0.0.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]
9+
enum_dispatch = "0.3.7"
10+
11+
[[bin]]
12+
name = "nested-submodules"
13+
test = false
14+
bench = false
15+
path = "src/nested-submodules.rs"
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
fn main() {
2+
my_module::print(my_module::submodule::MSG);
3+
}
4+
5+
mod my_module {
6+
pub fn print(msg: &str) {
7+
println!("{}", msg);
8+
}
9+
10+
pub mod submodule {
11+
pub const MSG: &str = "Hello world!";
12+
}
13+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "day-11-traffic-light"
3+
version = "0.0.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]
9+
enum_dispatch = "0.3.7"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
pub(crate) mod house_light;
2+
pub(crate) mod traffic_light;
3+
4+
pub(crate) trait Light {
5+
fn get_name(&self) -> &str;
6+
fn get_state(&self) -> &dyn std::fmt::Debug;
7+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
use std::fmt::Display;
2+
3+
use super::Light;
4+
5+
impl Light for HouseLight {
6+
fn get_name(&self) -> &str {
7+
"House light"
8+
}
9+
10+
fn get_state(&self) -> &dyn std::fmt::Debug {
11+
&self.on
12+
}
13+
}
14+
15+
#[derive(Debug)]
16+
pub(crate) struct HouseLight {
17+
on: bool,
18+
}
19+
20+
impl Display for HouseLight {
21+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
22+
write!(f, "Houselight is {}", if self.on { "on" } else { "off" })
23+
}
24+
}
25+
26+
impl HouseLight {
27+
pub fn new() -> Self {
28+
Self { on: false }
29+
}
30+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
use std::fmt::Display;
2+
3+
use super::Light;
4+
5+
impl Light for TrafficLight {
6+
fn get_name(&self) -> &str {
7+
"Traffic light"
8+
}
9+
10+
fn get_state(&self) -> &dyn std::fmt::Debug {
11+
&self.color
12+
}
13+
}
14+
15+
impl std::fmt::Display for TrafficLight {
16+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
17+
write!(f, "Traffic light is {}", self.color)
18+
}
19+
}
20+
21+
#[derive(Debug)]
22+
pub(crate) struct TrafficLight {
23+
color: TrafficLightColor,
24+
}
25+
26+
impl TrafficLight {
27+
pub fn new() -> Self {
28+
Self {
29+
color: TrafficLightColor::Red,
30+
}
31+
}
32+
33+
pub fn turn_green(&mut self) {
34+
self.color = TrafficLightColor::Green
35+
}
36+
}
37+
38+
#[derive(Debug)]
39+
enum TrafficLightColor {
40+
Red,
41+
Yellow,
42+
Green,
43+
}
44+
45+
impl Display for TrafficLightColor {
46+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
47+
let color_string = match self {
48+
TrafficLightColor::Green => "green",
49+
TrafficLightColor::Red => "red",
50+
TrafficLightColor::Yellow => "yellow",
51+
};
52+
write!(f, "{}", color_string)
53+
}
54+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use crate::light::house_light::HouseLight;
2+
use crate::light::traffic_light::TrafficLight;
3+
use crate::light::Light;
4+
5+
mod light;
6+
7+
fn main() {
8+
let traffic_light = TrafficLight::new();
9+
let house_light = HouseLight::new();
10+
11+
print_state(&traffic_light);
12+
print_state(&house_light);
13+
}
14+
15+
fn print_state(light: &impl Light) {
16+
println!("{}'s state is : {:?}", light.get_name(), light.get_state());
17+
}

0 commit comments

Comments
 (0)