Created
January 24, 2020 15:35
-
-
Save rust-play/db77f92bf206bc0191c1516e8c1ade8f to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
This file contains 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
struct Int32(i32); | |
struct Int64(i64); | |
pub trait Num { | |
fn new() -> Self; | |
} | |
impl Num for Int32 { | |
fn new() -> Self { Int32(0) } | |
} | |
impl Num for Int64 { | |
fn new() -> Self { Int64(0) } | |
} | |
fn my_function<T: Num>() -> T { | |
let my_num = T::new(); | |
// Do something with my_num | |
return my_num; | |
} | |
fn main() { | |
let i_32 = my_function::<Int32>(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment