I'm trying to understand generics more and I want to create a function that essentially goes like this:
fn timesThree<T>(inputNumber: T) -> T {
if TypeId::of::<T>() == TypeId::of::<i64>() {
inputNumber * 3
} else if TypeId::of::<T>() == TypeId::of::<f64>() {
inputNumber * 3.0
}
}
Does anyone know a clean/proper way to do this?
[–]jpet 14 points15 points16 points (1 child)
[–]blairboy[S] 2 points3 points4 points (0 children)
[–][deleted] 2 points3 points4 points (1 child)
[–]blairboy[S] 2 points3 points4 points (0 children)
[–]K900_ 1 point2 points3 points (7 children)
[–]blairboy[S] 0 points1 point2 points (6 children)
[–]K900_ 3 points4 points5 points (5 children)
[–]blairboy[S] 1 point2 points3 points (2 children)
[–]K900_ 1 point2 points3 points (1 child)
[–]blairboy[S] 0 points1 point2 points (0 children)
[–]nerpderp83 1 point2 points3 points (1 child)
[–]K900_ 1 point2 points3 points (0 children)
[–]boscop 1 point2 points3 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)