Skip to content

Commit 4c3b714

Browse files
authored
Create pattern-matching-with-match-in-rust.rs
1 parent 809f014 commit 4c3b714

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
struct Person {
2+
age: u8,
3+
}
4+
5+
fn describe_person(person: &Person) -> &str {
6+
match person.age {
7+
0..=12 => "Child",
8+
13..=17 => "Teenager",
9+
_ => "Adult",
10+
}
11+
}
12+
13+
fn main() {
14+
let foo = Person { age: 10 };
15+
// prints Foo is a Child
16+
println!("Foo is a {}", describe_person(&foo));
17+
let bar = Person { age: 13 };
18+
// prints Bar is a Teenager
19+
println!("Bar is a {}", describe_person(&bar));
20+
let baz = Person { age: 40 };
21+
// prints Baz is an Adult
22+
println!("Baz is an {}", describe_person(&baz));
23+
}

0 commit comments

Comments
 (0)