Fix the unused variable warning by wrapping the mutable vector in a RefCell to allow interior mutability within the Messenger trait implementation.
The warning occurs because the send method in your Messenger trait implementation takes &self but attempts to mutate sent_messages without interior mutability. Wrap the Vec in a RefCell and borrow it mutably inside the method.
use std::cell::RefCell;
struct MockMessenger {
sent_messages: RefCell<Vec<String>>,
}
impl Messenger for MockMessenger {
fn send(&self, message: &str) {
self.sent_messages.borrow_mut().push(String::from(message));
}
}
Rust prevents you from changing data through a read-only reference to ensure safety. In this case, you are trying to add a message to a list while only having read access to the object. Wrapping the list in a RefCell allows you to change the contents at runtime even when you only have a read-only reference to the outer object, similar to having a key that opens a locked box inside a room you can only look into.