mirror of
https://github.com/chubin/cheat.sheets
synced 2024-11-17 09:25:32 +00:00
21 lines
784 B
Plaintext
21 lines
784 B
Plaintext
let foo = OptionalI32::AnI32(1);
|
||
match foo {
|
||
OptionalI32::AnI32(n) => println!("it’s an i32: {}", n),
|
||
OptionalI32::Nothing => println!("it’s 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!"),
|
||
}
|