Created
November 18, 2018 19:59
-
-
Save rust-play/2755b5d510f6af102be3d64c6ac5c51b to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
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 A { | |
fn greet(&self) { | |
println!("Hi! I am {}!", self.name()); | |
} | |
fn name(&self) -> &str { | |
"A" | |
} | |
} | |
trait B: A { | |
fn name(&self) -> &str { | |
"B" | |
} | |
} | |
struct X { | |
name: &'static str, | |
} | |
struct Y {} | |
impl A for X {} | |
impl B for X { | |
fn name(&self) -> &str { | |
self.name | |
} | |
} | |
impl A for Y {} | |
impl B for Y {} | |
fn greet<T: B>(t: &T) { | |
t.greet(); | |
} | |
fn main() { | |
let x = X { name: "X" }; | |
let y = Y {}; | |
greet(&x); | |
greet(&y); | |
println!("{}", A::name(&x)); | |
println!("{}", B::name(&x)); | |
println!("{}", A::name(&y)); | |
println!("{}", B::name(&y)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment