Hello Rust friends. Is it bad to implement Add and then use this boilerplate for AddAssign? impl AddAssign for Foo { fn add_assign(&mut self, rhs: Self) { *self = *self + rhs; } }
-
Show this thread
-
Replying to @ReinH
It's not bad. AddAssign is just there to give people the opportunity to optimize for when you're modifying self instead of returning a new value. Definitely no requirement that you take advantage of that if you don't need/want to. :)
1 reply 0 retweets 1 like -
Replying to @Sunjay03
Are there any types that implement both Add and AddAssign for which the above would not be a valid impl of AddAssign, i.e., can this be treated as an AddAssign "law"?
1 reply 0 retweets 1 like -
It'd always be *valid*, but for any type which heap allocates it'd be pretty problematic
1 reply 0 retweets 3 likes
Maybe unpopular take: "If x += 1 is equivalent to x = x + 1 you should just write the latter"
4:11 PM - 28 Jul 2020
0 replies
0 retweets
1 like
Loading seems to be taking a while.
Twitter may be over capacity or experiencing a momentary hiccup. Try again or visit Twitter Status for more information.