Total newbie here, trying to create a simple generic function that returns a product of two numbers. The following two functions do work, but is there a simpler way to accomplish this:
fn product_ref<T>(x: &T, y: &T) -> T
where T: std::ops::Mul<Output = T>,
T: std::marker::Copy
{
(*x) * (*y)
}
fn product<T>(x: T, y: T) -> T
where T: std::ops::Mul<Output = T>,
T: std::marker::Copy
{
x * y
}
let x = 4;
let y = 5;
print!("product1={}\n", product(x, y));
print!("product2={}\n", product_ref( &x, &y));
One more thing: is there a way to combine several traits which are commonly used together in a thing called Number, so that we could just say where T: Number instead of listing that we can copy, add, multiply this T and so on. TIA!