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<dyn Foo>) { | |
println!("{}",x.method()); | |
} | |
fn main() { | |
//let x = &S1{a:5u8}; | |
//let y = &S2{a:"Hello".to_string()}; | |
let list_of_foo:Vec<Box<dyn Foo>> = 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!"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment