2
0
mirror of https://github.com/chubin/cheat.sheets synced 2024-11-12 19:10:26 +00:00
cheat.sheets/sheets/_rust/PatternMatching
2017-05-28 21:10:41 +00:00

21 lines
784 B
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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