You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We can’t modify the `MockMessenger` to keep track of the messages, because the
`send` method takes an immutable reference to `self`. We also can’t take the
suggestion from the error text to use `&mut self` instead, because then the
signature of `send` wouldn’t match the signature in the `Messenger` trait
definition (feel free to try and see what error message you get).
Description of the problem: Rust 1.82.0 (and possibly earlier) suggests changing the trait definition as well as the impl method, as below. This still would not compile, but the reason now becomes that messenger is contained as an immutable reference in LimitTracker, and so cannot be passed to send.
error[E0596]: cannot borrow `self.sent_messages` as mutable, as it is behind a `&` reference
--> src/sec_5.rs:62:13
|
62 | self.sent_messages.push(message.to_string());
| ^^^^^^^^^^^^^^^^^^ `self` is a `&` reference, so the data it refers to cannot be borrowed as mutable
|
help: consider changing this to be a mutable reference in the `impl` method and the `trait` definition
|
6 ~ fn send(&mut self, message: &str);
7 | }
...
60 | impl Messenger for MockMessenger {
61 ~ fn send(&mut self, message: &str) {
|
For more information about this error, try `rustc --explain E0596`.
Suggested fix: Perhaps change the second sentence to: "We also can’t take the suggestion from the error text to use &mut self instead, because our LimitTracker only holds an immutable reference to a Messenger instance (feel free to try and see what error message you get)." Or, to adress the concern that this might be changed in turn: "We could follow the compiler's suggestions to work with mutable references instead, but here we'll assume that we want the send method in our library to accept immutable references."
The text was updated successfully, but these errors were encountered:
It looks like this change landed in 1.79. I think the sentence could indeed use a tiny bit of tweaking, but I think in keeping with the actual flow of the text here, the point should be that we shouldn't be changing the trait definition for the sake of test code. I’ll open a small PR to that effect!
Previously, the error message from the compiler only suggested changing
the `impl` to use `&mut self`, but a (very helpful!) update to the error
message suggests changing both the `impl` and the `trait` definitions,
which is much better in the general case since changing one without the
other will just produce a new error message: a point the original text
points to with its suggestion to try and see what happens.
Account for the new error message by keeping the framing of the reason
as avoiding breaking the trait contract, but now explicitly in terms of
good testing habits.
Fixes#4140
milestone:ch15
main
branch to see if this has already been fixed, in this file:src/ch15-05-interior-mutability.md
URL to the section(s) of the book with this problem:
book/src/ch15-05-interior-mutability.md
Lines 190 to 194 in 139538d
Description of the problem: Rust 1.82.0 (and possibly earlier) suggests changing the trait definition as well as the
impl
method, as below. This still would not compile, but the reason now becomes thatmessenger
is contained as an immutable reference inLimitTracker
, and so cannot be passed tosend
.Suggested fix: Perhaps change the second sentence to: "We also can’t take the suggestion from the error text to use
&mut self
instead, because ourLimitTracker
only holds an immutable reference to aMessenger
instance (feel free to try and see what error message you get)." Or, to adress the concern that this might be changed in turn: "We could follow the compiler's suggestions to work with mutable references instead, but here we'll assume that we want thesend
method in our library to accept immutable references."The text was updated successfully, but these errors were encountered: