Skip to content

Instantly share code, notes, and snippets.

@mfpiccolo
Created September 26, 2015 20:08
Show Gist options
  • Save mfpiccolo/f5071c2d878e4299308a to your computer and use it in GitHub Desktop.
Save mfpiccolo/f5071c2d878e4299308a to your computer and use it in GitHub Desktop.
pub trait Converter {
fn blah(&self) -> &String;
}
struct This {
convert: String,
}
struct That {
convert: String,
}
impl Converter for This { fn blah(&self) -> &String { &self.convert } }
impl Converter for That { fn blah(&self) -> &String { &self.convert } }
fn convert_to(s: &str) -> Option<Box<Converter+'static>> {
match s {
"this" => Some(Box::new(This {convert: "converted!".to_string()})),
"that" => Some(Box::new(That {convert: "converted!".to_string()})),
_ => None
}
}
fn main() {
let x = convert_to("this").unwrap();
let y = convert_to("that").unwrap();
println!("{:?}", x.blah());
println!("{:?}", y.blah());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment