2
0
mirror of https://github.com/chubin/cheat.sheets synced 2024-11-09 07:10:36 +00:00
cheat.sheets/sheets/_rust/PatternMatching

21 lines
784 B
Plaintext
Raw Normal View History

2017-05-28 21:10:41 +00:00
let foo = OptionalI32::AnI32(1);
match foo {
OptionalI32::AnI32(n) => println!("its an i32: {}", n),
OptionalI32::Nothing => println!("its nothing!"),
}
// Advanced pattern matching
struct FooBar { x: i32, y: OptionalI32 }
let bar = FooBar { x: 15, y: OptionalI32::AnI32(32) };
//
match bar {
FooBar { x: 0, y: OptionalI32::AnI32(0) } =>
println!("The numbers are zero!"),
FooBar { x: n, y: OptionalI32::AnI32(m) } if n == m =>
println!("The numbers are the same"),
FooBar { x: n, y: OptionalI32::AnI32(m) } =>
println!("Different numbers: {} {}", n, m),
FooBar { x: _, y: OptionalI32::Nothing } =>
println!("The second number is Nothing!"),
}