Last active
August 29, 2015 14:24
-
-
Save nrc/7261e80068115a3ac76c to your computer and use it in GitHub Desktop.
Method calls
This file contains hidden or 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
// I think this is every possible way to call a method in Rust. | |
struct Foo; | |
impl Foo { | |
fn m1() { | |
println!("Hello! m1"); | |
} | |
fn m2(&self) { | |
println!("Hello! m2"); | |
} | |
} | |
trait Bar { | |
fn m3(); | |
fn m4(&self); | |
} | |
impl Bar for Foo { | |
fn m3() { | |
println!("Hello! m3"); | |
} | |
fn m4(&self) { | |
println!("Hello! m4"); | |
} | |
} | |
trait Baz { | |
fn m5(&self); | |
} | |
impl Baz for Foo { | |
fn m5(&self) { | |
println!("Hello! m5"); | |
} | |
} | |
fn foo<T: Bar>(x: T) { | |
x.m4(); | |
} | |
fn qux<T: Baz + ?Sized>(x: &T) { | |
x.m5(); | |
} | |
fn main() { | |
// Inherant | |
Foo::m1(); | |
// Inherant with receiver | |
Foo.m2(); | |
// Static | |
Foo::m3(); | |
// UFCS static | |
<Foo as Bar>::m3(); | |
// Static with receiver | |
Foo.m4(); | |
// UFCS static with receiver | |
Foo::m4(&Foo); | |
let x: &Baz = &Foo; | |
// Dynamic | |
x.m5(); | |
// UFCS dynamic | |
Baz::m5(x); | |
// UFCS static | |
<Foo as Baz>::m5(&Foo); | |
// Static vtable | |
foo(Foo); | |
// Dynamic vtable | |
qux(x); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment