Last active
December 29, 2018 13:05
-
-
Save Rahix/75e9bfc17aa30834d752f80672930b0e to your computer and use it in GitHub Desktop.
Static and Dynamic Dispatch at the same time
This file contains hidden or 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
trait Foo { | |
fn bar(&self); | |
} | |
impl Foo for &dyn Foo { | |
fn bar(&self) { | |
(*self).bar(); | |
} | |
} | |
struct Bar; | |
impl Foo for Bar { | |
fn bar(&self) { | |
println!("Hello"); | |
} | |
} | |
fn foo<T: Foo>(f: &T) { | |
f.bar(); | |
} | |
fn baz(f: &dyn Foo) { | |
foo(&f) | |
} | |
fn main() { | |
let b = Bar; | |
foo(&b); | |
baz(&b); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Actually this is better: