Question

I'm trying to write a generic function summ in rust - but to no avail. Could someone please elucidate the problem?

fn summ<T:Add>(a:T,b:T)->T  {
    a+b
} 
Was it helpful?

Solution

Add's add method does not return Self - it returns Self::Output. This allows the addition to return a different type than the addends. The return type of summ should reflect that:

fn summ<T: Add>(a: T, b: T) -> T::Output  {
    a + b
}

OTHER TIPS

I don't know much about Rust, but I'm assuming that since there are no constraints on T, there's no way to know that it even has a + operator. You should probably constrain T to implement std::ops::Add.

Licensed under: CC-BY-SA with attribution
scroll top