Last active
January 8, 2017 10:47
-
-
Save dumindu/51e38aca2b8243f3c4605181db61ca1d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
enum FlashMessage { | |
Success, //a unit variant | |
Warning{ category: i32, message: String }, //a struct variant | |
Error(String) //a tuple variant | |
} | |
fn main() { | |
let mut form_status = FlashMessage::Success; | |
print_flash_message(form_status); | |
form_status = FlashMessage::Warning {category: 2, message: String::from("Field X is required")}; | |
print_flash_message(form_status); | |
form_status = FlashMessage::Error(String::from("Connection Error")); | |
print_flash_message(form_status); | |
} | |
fn print_flash_message(m : FlashMessage) { | |
// pattern matching with enum | |
match m { | |
FlashMessage::Success => | |
println!("Form Submitted correctly"), | |
FlashMessage::Warning {category, message} => //Destructure, should use same field names | |
println!("Warning : {} - {}", category, message), | |
FlashMessage::Error(msg) => | |
println!("Error : {}", msg) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment