Confusion about the add operate

image
why the i32 reference can add with i32

That works because there is an implementation of the Add trait between a i32 and &i32. The implementation is exactly as you would expect - first we dereference the &i32 to get a i32, then we add the two i32s together like normal.

1 Like

I think you linked the wrong implementation. It is actually this one.

1 Like

See also: Surprising: &1 + &1 == 2

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.