Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

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
} 
share|improve this question

closed as unclear what you're asking by gnat, Michael Kohne, MichaelT, GlenH7, Kilian Foth Jun 23 at 10:36

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

    
recommended reading: Which computer science / programming Stack Exchange do I post in? –  gnat Jun 22 at 14:50

2 Answers 2

up vote 1 down vote accepted

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
}
share|improve this answer
    
thanx a lot - it works like a charm . –  tyro1 Jun 22 at 20:05

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.

share|improve this answer
    
well good sugestion - but not necessary in my case - thanx. –  tyro1 Jun 22 at 20:08

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