3 ms·
To say it was equivalent to b.add(a) was incorrect and unwise. But to say a.add(b) or Add::add(a, b) was correct. It used to be more complex in the distant past
by chrismorgan 8y ago
To say it was equivalent to b.add(a) was incorrect and unwise. But to say a.add(b) or Add::add(a, b) was correct. It used to be more complex in the distant past when the operators worked by reference, but since some time before 1.0 they operate by value, and so the following are all equivalent:
• `a + b`
• `a.add(b)` (provided std::ops::Add is imported and the type of a doesn’t have an intrinsic method named add, which would take precedence)
• `Add::add(a, b)` (provided std::ops::Add is imported)
• `<A as ::std::ops::Add<B>>::add(a, b)`, where A and B are the types of a and b (provided we’re not no_std).
(Note that `a.add(b)` would work if a is of type A and &A implements Add but A doesn’t, because of autoref/autoderef in method calls, while the other three approaches would not work, and would instead want you to write &a.)
Demonstration of most of this: https://play.rust-lang.org/?gist=c70a8c62fa0f304b1a8dceddf98302f1 https://play.rust-lang.org/?gist=c70a8c62fa0f304b1a8dceddf98...
- cesarb 8y ago> (Note that `a.add(b)` would work if a is of type A and &A implements Add but A doesn’t, because of autoref/autoderef in method calls, while the other three approaches would not work, and would instead want you to write &a.) That implies to me that "`Add::add(a, b)` (provided std::ops::Add is imported)" would be the most didactic explanation.