Created
May 16, 2019 10:32
-
-
Save king6cong/d2664a8e5c2edb21b4c41132a0afffca to your computer and use it in GitHub Desktop.
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
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