matching and patterns

这个提交包含在:
gbrochar 2020-12-08 19:10:37 +01:00
父节点 6334e5627a
当前提交 191e5af4c7
共有 2 个文件被更改,包括 44 次插入0 次删除

9
matching/Cargo.toml 普通文件
查看文件

@ -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 普通文件
查看文件

@ -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"),
}
}