Skip to content

Instantly share code, notes, and snippets.

@cristianoliveira
Created June 15, 2016 14:54
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cristianoliveira/722204361927761ae69c150fde040059 to your computer and use it in GitHub Desktop.
Save cristianoliveira/722204361927761ae69c150fde040059 to your computer and use it in GitHub Desktop.
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