Skip to content

Instantly share code, notes, and snippets.

@kilroyjones
Last active July 30, 2020 00:15
Show Gist options
  • Save kilroyjones/a8738bb9c1d5e89b61ee8ae276ab8030 to your computer and use it in GitHub Desktop.
Save kilroyjones/a8738bb9c1d5e89b61ee8ae276ab8030 to your computer and use it in GitHub Desktop.
Rust add function using any parameter type.
extern crate num;
use num::{ToPrimitive, Float};
fn add<T, R, U>(a: T, b: R) -> U
where
T: ToPrimitive,
R: ToPrimitive,
U: Float,
{
U::from(a).unwrap() + U::from(b).unwrap()
}
fn main() {
let b:f64 = add(8, 7.76);
let b:f32 = add(8, 8);
println!("{}", b);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment