Skip to content

Instantly share code, notes, and snippets.

@jeffs

jeffs/draw.rs Secret

Last active August 23, 2021 03:19
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 jeffs/198793eacf93f354e749cf83681daf83 to your computer and use it in GitHub Desktop.
Save jeffs/198793eacf93f354e749cf83681daf83 to your computer and use it in GitHub Desktop.
nested-polymorphism-virtual-rs
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());
}
$ 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