matching and patterns

This commit is contained in:
gbrochar 2020-12-08 19:10:37 +01:00
parent 6334e5627a
commit 191e5af4c7
2 changed files with 44 additions and 0 deletions

9
matching/Cargo.toml Normal file
View File

@ -0,0 +1,9 @@
[package]
name = "matching"
version = "0.1.0"
authors = ["gbrochar <gaetanbrochard@protonmail.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

35
matching/src/main.rs Normal file
View File

@ -0,0 +1,35 @@
struct Point {
x: i32,
y: i32,
}
fn main() {
let mut stack = Vec::new();
stack.push(1);
stack.push(2);
stack.push(3);
while let Some(top) = stack.pop() {
println!("{}", top);
}
let p = Point { x: 0, y: 7 };
match p {
Point { x, y: 0 } => println!("On the x axis at {}", x),
Point { x: 0, y } => println!("On the y axis at {}", y),
Point { x, y } => println!("On neither axis: ({}, {})", x, y),
}
let ((feet, inches), Point { x, y }) = ((3, 10), Point { x: 3, y: -10 });
println!("{}, {}, {}, {}", feet, inches, x, y);
let x = 4;
let y = false;
match x {
4 | 5 | 6 if y => println!("yes"),
_ => println!("no"),
}
}