-
-
Save jeffs/198793eacf93f354e749cf83681daf83 to your computer and use it in GitHub Desktop.
nested-polymorphism-virtual-rs
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 Draw { | |
fn draw(&self) -> &'static str; | |
} | |
struct BlackjackDealer; | |
struct Cartoonist; | |
impl Draw for BlackjackDealer { | |
fn draw(&self) -> &'static str { | |
"🃏" | |
} | |
} | |
impl Draw for Cartoonist { | |
fn draw(&self) -> &'static str { | |
"🖼️" | |
} | |
} | |
fn main() { | |
let subject = match std::env::args().nth(1) { | |
Some(s) if s == "BlackjackDealer" => &BlackjackDealer as &dyn Draw, | |
Some(s) if s == "Cartoonist" => &Cartoonist as &dyn Draw, | |
_ => { | |
eprintln!("usage: draw {{BlackjackDealer|Cartoonist}}"); | |
std::process::exit(2); | |
} | |
}; | |
println!("{}", subject.draw()); | |
} |
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
$ cargo run --quiet --bin draw BlackjackDealer | |
🃏 | |
$ cargo run --quiet --bin draw Cartoonist | |
🖼️ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment