Skip to content

Instantly share code, notes, and snippets.

@urbanautomaton
Last active July 1, 2019 07:04
Show Gist options
  • Save urbanautomaton/b22af352591c2c68760d2c8763dfc69f to your computer and use it in GitHub Desktop.
Save urbanautomaton/b22af352591c2c68760d2c8763dfc69f to your computer and use it in GitHub Desktop.
trait A {}
trait B: A {}
struct JustA {}
impl A for JustA {}
struct AandB {}
impl A for AandB {}
impl B for AandB {}
fn build_just_a() -> Box<A> {
Box::new(JustA {})
}
fn build_a_and_b() -> Box<B> {
Box::new(AandB {})
}
fn main() {
let aaa: Vec<Box<A>> = vec![build_just_a(), build_a_and_b()];
println!("aaa: {}", aaa.len());
}
Compiling trait_inheritance v0.1.0 (/Users/simon/tmp/trait_inheritance)
error[E0308]: mismatched types
--> src/main.rs:21:44
|
21 | let aaa: Vec<Box<A>> = vec![build_a(), build_a_and_b()];
| ^^^^^^^^^^^^^^^ expected trait `A`, found trait `B`
|
= note: expected type `std::boxed::Box<dyn A>`
found type `std::boxed::Box<(dyn B + 'static)>`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0308`.
error: Could not compile `trait_inheritance`.
To learn more, run the command again with --verbose.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment