Created
June 8, 2018 12:53
-
-
Save o0Ignition0o/73cde984e5cb6559327fd16bff6ccf32 to your computer and use it in GitHub Desktop.
impl trait vec !
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
trait MakesNoise { | |
fn make_noise(self) -> (); | |
} | |
struct Dog; | |
struct Cat; | |
impl MakesNoise for Dog { | |
fn make_noise(self) -> () { | |
println!("WOOF WOOF"); | |
// Who let the dogs out ? | |
} | |
} | |
impl MakesNoise for Cat { | |
fn make_noise(self) -> () { | |
println!("Everybody wants to be a cat !"); | |
// Because the cat's the only cat, who knows where it's at | |
} | |
} | |
fn make_some_noise(animals: Vec<impl MakesNoise>) -> () { | |
for thing in animals { | |
thing.make_noise(); | |
} | |
} | |
fn main() -> () { | |
let a = Dog {}; | |
let b = Cat {}; | |
let animals: Vec<impl MakesNoise> = vec![a, b]; // `impl Trait` not allowed outside of function and inherent method return types | |
make_some_noise(animals); | |
a.make_noise(); | |
b.make_noise(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment