trait Foo { fn method(&self) -> String; } struct S1 { a: u8, } struct S2 { a: String, } impl Foo for S1 { fn method(&self) -> String { format!("u8: {}", self.a) } } impl Foo for S2 { fn method(&self) -> String { format!("string: {}", self.a) } } fn do_something(x: &Box) { println!("{}",x.method()); } fn main() { //let x = &S1{a:5u8}; //let y = &S2{a:"Hello".to_string()}; let list_of_foo:Vec> = vec![ Box::new(S1{a:5u8}), Box::new(S2{a:"Hello".to_string()})]; for i in list_of_foo.iter() { do_something(i); } println!("Hello, world!"); }