Last active
March 8, 2016 13:53
-
-
Save zeroem/3a7bcbb713ac75ae0707 to your computer and use it in GitHub Desktop.
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 Trait { | |
fn print(&mut self); | |
} | |
struct ImplA { | |
s: String, | |
} | |
impl Trait for ImplA { | |
fn print(&mut self) { | |
println!("A: {:?}", self.s); | |
} | |
} | |
struct ImplB { | |
s: String, | |
} | |
impl Trait for ImplB { | |
fn print(&mut self) { | |
println!("B: {:?}", self.s); | |
} | |
} | |
fn main() { | |
let box_a = &mut Box::new(ImplA {s: String::from("foobar")}); | |
let box_b = &mut Box::new(ImplB {s: String::from("foobar")}); | |
let v: Vec<&mut Box<Trait>> = vec!(box_a, box_b); | |
for f in v { | |
f.print(); | |
} | |
} |
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
zeroem@whizbang:~/projects/rust-play$ rustc vec-of-mutable-traits.rs | |
vec-of-mutable-traits.rs:29:40: 29:45 error: mismatched types: | |
expected `&mut Box<Trait>`, | |
found `&mut Box<ImplA>` | |
(expected trait Trait, | |
found struct `ImplA`) [E0308] | |
vec-of-mutable-traits.rs:29 let v: Vec<&mut Box<Trait>> = vec!(box_a, box_b); | |
^~~~~ | |
vec-of-mutable-traits.rs:29:35: 29:53 note: in this expansion of vec! (defined in <std macros>) | |
vec-of-mutable-traits.rs:29:40: 29:45 help: run `rustc --explain E0308` to see a detailed explanation | |
vec-of-mutable-traits.rs:29:47: 29:52 error: mismatched types: | |
expected `&mut Box<Trait>`, | |
found `&mut Box<ImplB>` | |
(expected trait Trait, | |
found struct `ImplB`) [E0308] | |
vec-of-mutable-traits.rs:29 let v: Vec<&mut Box<Trait>> = vec!(box_a, box_b); | |
^~~~~ | |
vec-of-mutable-traits.rs:29:35: 29:53 note: in this expansion of vec! (defined in <std macros>) | |
vec-of-mutable-traits.rs:29:47: 29:52 help: run `rustc --explain E0308` to see a detailed explanation | |
error: aborting due to 2 previous errors |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment