Created
December 12, 2018 14:36
-
-
Save rust-play/200b460f310015af02ed9bb494e000ad to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
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
trait TSociable{ | |
fn say(&self); | |
fn instance() -> Box<dyn TSociable> { | |
if true { Hello::new() } else { GoodBye::new() } | |
} | |
} | |
struct Hello; | |
impl Hello { | |
fn new() -> Box<Hello> { | |
Box::new(Hello) | |
} | |
} | |
struct GoodBye; | |
impl GoodBye { | |
fn new() -> Box<GoodBye> { | |
Box::new(GoodBye) | |
} | |
} | |
impl TSociable for Hello { fn say(&self) { println!("Hello"); } } | |
impl TSociable for GoodBye { fn say(&self) { println!("GoodBye"); } } | |
fn main() { | |
let mut somenthing = TSociable::instance(); | |
let mut somenthing: Box<TSociable>; | |
somenthing = Hello::new(); | |
somenthing.say(); | |
somenthing = GoodBye::new(); | |
somenthing.say(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment