0

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
} 
1

2 Answers 2

2

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
}
1
  • thanx a lot - it works like a charm .
    – tyro1
    Commented Jun 22, 2015 at 20:05
0

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.

1
  • well good sugestion - but not necessary in my case - thanx.
    – tyro1
    Commented Jun 22, 2015 at 20:08

Not the answer you're looking for? Browse other questions tagged or ask your own question.