Skip to content

Instantly share code, notes, and snippets.

@zeroem
Last active March 8, 2016 13:53
Show Gist options
  • Save zeroem/3a7bcbb713ac75ae0707 to your computer and use it in GitHub Desktop.
Save zeroem/3a7bcbb713ac75ae0707 to your computer and use it in GitHub Desktop.
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();
}
}
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