| // Generic Programming: Dynamic Polymorphism |
| |
| #![allow(dead_code)] |
| #![allow(unused_imports)] |
| |
| use rust_tutorial::*; |
| |
| fn stringify_all(xs: Vec<&dyn ToString>) -> String { |
| let strings: Vec<String> = |
| xs.iter().map(|x| x.to_string()).collect(); |
| |
| strings.join(", ") |
| } |
| |
| fn main() { |
| let mut stringlike_things: Vec<&dyn ToString> = Vec::new(); |
| |
| let x1 = "abc"; |
| let x2 = 100; |
| let x3 = Person { age: 30, name: "Mary".to_string() }; |
| |
| stringlike_things.push(&x1); |
| stringlike_things.push(&x2); |
| stringlike_things.push(&x3); |
| |
| println!("{}", stringify_all(stringlike_things)); |
| } |