Is the + operator sugar code?

67 points NormalAppearance2851 16 comments

In core there exists the Add trait which enables adding two types together. By default, primitive types and their borrowed forms are implemented.

If i implement this trait for my own type, type X for the example's sake, i will be able to do:

let x1 =  X::new();
let x2 =  X::new();
let x_final = x1 + x2;

So is + just sugar code? Similar to for? AFAIK let x_final = x1 + x2; expands to let x_final = x1.add(x2);

Ok. If x really is sugar code, then how does "real" addition happen? For example, what's the implementation code for Add<i8> for i8 ?

I looked at the source and... it's just this...?

fn add(self, other: i8) -> i8 { self + other } // Slightly simplified for obviousy reasons

So i8 + i8 expands to i8 + i8? I'm EXTREMELY confused. Am i even looking at the right places? Is my understanding flawed?