Last active
September 13, 2022 15:32
-
-
Save gftea/86f4226c875150278d14a18104d6c303 to your computer and use it in GitHub Desktop.
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
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