79721860

Date: 2025-07-31 21:41:12
Score: 2
Natty:
Report link

I am posting this answer not as away of negating the previous answer given (which i accepted) but to give my take on what i have leanrt so farespecially after doing somwhat of a 'revision'.

I wrote similar code to the one in my question recently like thus:

// Add two numbers (integer and float, float and integer, float and float or integer and integer)
// and output the result as a float
fn add<T, U>(x: T, y: U) -> f64
where T: From<i32> + Into<f64>,
    U: From<i32> + Into<f64>
{
    x.into() + y.into()
}


fn main() {
    let x = add(5.0, 7);
    println!("{x:?}");
}

This compiled!! The catch of course is that regardless of what type you put as first argument or second they both must be convertible to float, should be able to be derived from an integer and the return type will be a float as well. I guess its not pure generics in play, but considering that (from my understanding) that concrete types like integer and float implement the trait From<i32> and Into<f64> (by integer i mean i32 and by float i mean f64), tghe function so far accepts both i32and i32, i32 and f64, f64 and i32 and f64 and f64. I am open to corrections and even criticisms. And if this code breaks still please let me know.

Reasons:
  • RegEx Blacklisted phrase (2.5): please let me know
  • Long answer (-1):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (0.5):
Posted by: DammyD