Created
June 15, 2016 14:54
-
-
Save cristianoliveira/722204361927761ae69c150fde040059 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
pub trait Command { | |
fn execute(&self) -> String; | |
} | |
struct AddCmd; | |
struct DeleteCmd; | |
impl Command for AddCmd { | |
fn execute(&self) -> String { "It add".into() } | |
} | |
impl Command for DeleteCmd { | |
fn execute(&self) -> String { "It delete".into() } | |
} | |
fn command(s: &str) -> Option<Box<Command+'static>> { | |
match s { | |
"add" => Some(Box::new(AddCmd)), | |
"delete" => Some(Box::new(DeleteCmd)), | |
_ => None | |
} | |
} | |
fn main() { | |
let a = command("add").unwrap(); | |
let d = command("delete").unwrap(); | |
println!("{}", a.execute()); | |
println!("{}", d.execute()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment