Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created November 18, 2018 19:59
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 rust-play/2755b5d510f6af102be3d64c6ac5c51b to your computer and use it in GitHub Desktop.
Save rust-play/2755b5d510f6af102be3d64c6ac5c51b to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
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