Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created July 18, 2020 11:58
Show Gist options
  • Save rust-play/12cef665676df085741c41725d94752f to your computer and use it in GitHub Desktop.
Save rust-play/12cef665676df085741c41725d94752f to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
trait A{
fn a(&self);
}
trait B{
fn b(&self);
}
impl B for A{
fn b(&self){
println!("B1");
self.a();
}
}
impl B for &A{
fn b(&self){
println!("B2");
(*self).a();
}
}
struct S;
impl A for S{
fn a(&self){
println!("S");
}
}
fn t<T: B+?Sized>(a: &T) {
a.b();
}
fn main() {
let a:&dyn A = &S;
let b:&dyn B = &a;
b.b();
t(a);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment