Skip to content

Instantly share code, notes, and snippets.

@king6cong
Created May 16, 2019 10:32
Show Gist options
  • Save king6cong/d2664a8e5c2edb21b4c41132a0afffca to your computer and use it in GitHub Desktop.
Save king6cong/d2664a8e5c2edb21b4c41132a0afffca to your computer and use it in GitHub Desktop.
fn di_dynamic() {
struct S;
struct P;
trait T {
fn name(&self);
}
impl T for S {
fn name(&self) {
println!("S");
}
}
impl T for P {
fn name(&self) {
println!("P");
}
}
let mut container: Vec<Box<T>> = vec![];
container.push(Box::new(S));
container.push(Box::new(P));
for trait_obj in container {
trait_obj.name();
}
}
fn di_static() {
struct S;
struct P;
trait T {
fn name();
}
impl T for S {
fn name() {
println!("S");
}
}
impl T for P {
fn name() {
println!("P");
}
}
macro_rules! types_call {
( $($v:ident),* )=> {
$(
$v::name();
)*
}
}
types_call!(S, P);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment