[ACCEPTED]-Why does the binary + operator not work with two &mut int?-rust
number + number
fails because they're two references, not 14 two ints. The compiler also tells us why: the 13 +
operator isn't implemented for the type 12 &mut int
.
You have to dereference with the dereference 11 operator *
to get at the int. This would 10 work let sum = *number + *number;
number.add(number);
works because the signature of add 9 is fn add(&self, &int) -> int;
Am I right to assume that test is still 8 5 because the data is copied over to increment? The 7 only way that the original test variable 6 could be mutated by the increment function 5 would be if it was send as Box, right?
Test 4 is not copied over, but is still 5 because 3 it's never actually mutated. You could mutate 2 it in the increment function if you wanted.
PS: to 1 mutate it
fn increment(number: &mut int) {
*number = *number + *number;
println!("{}", number);
}
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.