Skip to content

Instantly share code, notes, and snippets.

@gftea
Last active September 13, 2022 15:32
Show Gist options
  • Save gftea/86f4226c875150278d14a18104d6c303 to your computer and use it in GitHub Desktop.
Save gftea/86f4226c875150278d14a18104d6c303 to your computer and use it in GitHub Desktop.
trait Hello {
fn say_hello() -> Self;
}
impl Hello for i32 {
fn say_hello() -> i32 {
println!("hello i32");
0i32
}
}
impl Hello for usize {
fn say_hello() -> usize {
println!("hello usize");
0usize
}
}
fn main() {
// by annotating the return type is `i32`,
// compiler can resolve to the trait implementation of `i32`
let res: i32 = Hello::say_hello();
// by annotating the return type is `usize`,
// compiler can resolve to the trait implementation of `usize`
let res: usize = Hello::say_hello();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment