Skip to content

Commit 839ae34

Browse files
committed
added day 13
1 parent def1a3d commit 839ae34

File tree

8 files changed

+191
-0
lines changed

8 files changed

+191
-0
lines changed

Cargo.lock

Lines changed: 77 additions & 0 deletions
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
@@ -58,3 +58,14 @@ Modules
5858

5959
- `cargo run -p day-11-modules --bin nested-submodules`
6060
- `cargo run -p day-11-traffic-light`
61+
62+
### Day 12
63+
64+
- `cargo run -p day-12-impl-tostring`
65+
- `cargo run -p day-12-impl-asref-str`
66+
67+
### Day 13
68+
69+
- `cargo run -p day-13-option`
70+
- `cargo run -p day-13-result`
71+
- `cargo run -p day-13-question-mark`

crates/day-13/option/Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "day-13-option"
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]

crates/day-13/option/src/main.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
use std::time::Instant;
2+
3+
fn main() {
4+
let some = returns_some();
5+
println!("{:?}", some);
6+
7+
let none = returns_none();
8+
println!("{:?}", none);
9+
10+
let default_string = "Default value".to_owned();
11+
12+
let unwrap_or = returns_none().unwrap_or(default_string);
13+
println!("returns_none().unwrap_or(...): {:?}", unwrap_or);
14+
15+
let unwrap_or_else = returns_none()
16+
.unwrap_or_else(|| format!("Default value from a function at time {:?}", Instant::now()));
17+
18+
println!(
19+
"returns_none().unwrap_or_else(|| {{...}}): {:?}",
20+
unwrap_or_else
21+
);
22+
23+
let unwrap_or_default = returns_none().unwrap_or_default();
24+
25+
println!(
26+
"returns_none().unwrap_or_default(): {:?}",
27+
unwrap_or_default
28+
);
29+
30+
let match_value = match returns_some() {
31+
Some(val) => val,
32+
None => "My default value".to_owned(),
33+
};
34+
35+
println!("match {{...}}: {:?}", match_value);
36+
37+
if let Some(val) = returns_some() {
38+
println!("if let : {:?}", val);
39+
}
40+
}
41+
42+
fn returns_some() -> Option<String> {
43+
Some("my string".to_owned())
44+
}
45+
46+
fn returns_none() -> Option<String> {
47+
None
48+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "day-13-question-mark"
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+
markdown = "0.3.0"
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
use std::fs::read_to_string;
2+
3+
fn main() -> Result<(), std::io::Error> {
4+
let html = render_markdown("./README.md")?;
5+
println!("{}", html);
6+
Ok(())
7+
}
8+
9+
fn render_markdown(file: &str) -> Result<String, std::io::Error> {
10+
let source = read_to_string(file)?;
11+
Ok(markdown::to_html(&source))
12+
}

crates/day-13/result/Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "day-13-result"
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]

crates/day-13/result/src/main.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
fn main() {
2+
let value = returns_ok();
3+
println!("{:?}", value);
4+
5+
let value = returns_err();
6+
println!("{:?}", value);
7+
}
8+
9+
fn returns_ok() -> Result<String, MyError> {
10+
Ok("This turned out great!".to_owned())
11+
}
12+
13+
fn returns_err() -> Result<String, MyError> {
14+
Err(MyError("This failed horribly.".to_owned()))
15+
}
16+
17+
#[derive(Debug)]
18+
struct MyError(String);

0 commit comments

Comments
 (0)