Created
July 23, 2021 16:08
-
-
Save aaripurna/d40f9ec256a421f1e098b112b1cf2d57 to your computer and use it in GitHub Desktop.
Struct pattern patching
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Destructuring a struct | |
Person { | |
name: person_name, | |
age: person_age, | |
... | |
} = person; | |
println!("{}", person_name); | |
// Pattern match enum | |
match status { | |
Status::Paid => println!("paid!"), | |
Status::Pending => println!("pending"), | |
_ => println!("Other status") | |
} | |
// Pattern match struct | |
match person { | |
Person { age: 10, ... } => println!("Young"), | |
Person { name: "Simon", ... } => println!("One Direction"), | |
_ => println!("Who dis?") | |
} | |
// Pattern match tuples | |
let (a, b) = (1, 2); | |
match x { | |
(2, b) => println!("{}", b), | |
(a, 10) => println("{}", a), | |
_ => println!("Not as expected") | |
} | |
// and some other possible pattern match |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment