Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created January 24, 2020 15:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rust-play/db77f92bf206bc0191c1516e8c1ade8f to your computer and use it in GitHub Desktop.
Save rust-play/db77f92bf206bc0191c1516e8c1ade8f to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
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