Created
September 26, 2015 20:08
-
-
Save mfpiccolo/f5071c2d878e4299308a to your computer and use it in GitHub Desktop.
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
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