Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created December 12, 2018 14:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rust-play/200b460f310015af02ed9bb494e000ad to your computer and use it in GitHub Desktop.
Save rust-play/200b460f310015af02ed9bb494e000ad to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
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