Skip to content

Instantly share code, notes, and snippets.

@Clivern
Created August 16, 2021 21:48
Show Gist options
  • Save Clivern/d5b401ed7e1cfc69060461d4431b3c0e to your computer and use it in GitHub Desktop.
Save Clivern/d5b401ed7e1cfc69060461d4431b3c0e to your computer and use it in GitHub Desktop.
Rust Generics
fn sum<T: std::ops::Add<Output = T>>(n1: T, n2: T) -> T {
n1 + n2
}
fn max<T: std::cmp::PartialOrd>(n1: T, n2: T) -> T {
if n1 > n2 { n1 } else { n2 }
}
fn main() {
println!("{}", sum(2, 3));
println!("{}", max(2, 3));
}
@Clivern
Copy link
Author

Clivern commented Aug 16, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment