Created
January 24, 2020 06:05
-
-
Save rust-play/693569af19bb1124f2156f1f2b0fafa2 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 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