Skip to content

Commit 4127d68

Browse files
committed
initial code for c18 patterns and matching
1 parent 3d40e40 commit 4127d68

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

c18_pattern_matching/Cargo.toml

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

c18_pattern_matching/src/main.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
fn main() {
2+
let mut x = Some(1);
3+
//let mut x: Option<i32> = None;
4+
println!("i={:?}", x);
5+
//let x = ();
6+
x = match x {
7+
None => None,
8+
Some(i) => Some(i + 1),
9+
};
10+
println!("i={:?}", x);
11+
12+
println!("Hello, world!");
13+
14+
let favorite_color: Option<&str> = None;
15+
let is_tuesday = false;
16+
let age: Result<u8, _> = "34".parse();
17+
18+
if let Some(color) = favorite_color {
19+
println!("Using your favorite color, {color}, as the background");
20+
} else if is_tuesday {
21+
println!("Tuesday is green day!");
22+
} else if let Ok(age) = age {
23+
if age > 30 {
24+
println!("Using purple as the background color");
25+
} else {
26+
println!("Using orange as the background color");
27+
}
28+
} else {
29+
println!("Using blue as the background color");
30+
}
31+
}

0 commit comments

Comments
 (0)