Skip to content

Instantly share code, notes, and snippets.

@o0Ignition0o
Created June 8, 2018 12:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save o0Ignition0o/73cde984e5cb6559327fd16bff6ccf32 to your computer and use it in GitHub Desktop.
Save o0Ignition0o/73cde984e5cb6559327fd16bff6ccf32 to your computer and use it in GitHub Desktop.
impl trait vec !
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