Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created January 24, 2020 06:05
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/693569af19bb1124f2156f1f2b0fafa2 to your computer and use it in GitHub Desktop.
Save rust-play/693569af19bb1124f2156f1f2b0fafa2 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
trait Show {
fn show(&self) -> String;
}
impl Show for i32 {
fn show(&self) -> String {
format!("4 byte signed: {}", self)
}
}
impl Show for f64 {
fn show(&self) -> String {
format!("8 byte float: {}", self)
}
}
fn main() {
let answer = Box::new(42);
let maybe_pi = Box::new(3.14);
let show_list: Vec<Box<dyn Show>> = vec![answer, maybe_pi];
for d in show_list.iter() {
println!("show {}", d.show());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment