Hello, this is my first topic
I'm struggling with a borrowing error in my tiny code. I have a User
struct that has a name
field. The set_name
function of the User
struct changes the name
value to a new name and always returns true
.
struct User {
name: Option<String>,
}
impl User {
fn new() -> Self {
Self { name: None }
}
fn set_name(&mut self, new_name: &str) -> bool {
self.name = Some(new_name.to_string());
true
}
}
The User
struct is wrapped by UserService
struct that has a user
field. The set_user
function creates a new user instance and sets its name
to "Cake".
struct UserService {
user: Option<User>,
}
impl UserService {
fn new() -> Self {
Self { user: None }
}
fn set_user(&mut self) -> bool {
self.user = Some(User::new());
self.user.as_ref().unwrap().set_name("Cake")
}
}
The main
function just creates a new UserService
instance and prints a return value of the set_user
function that is expected to be true
.
fn main() {
let mut user_service = UserService::new();
println!("{}", user_service.set_user());
}
However, I got a cannot borrow data in a
& reference as mutable
error.
Compiling playground v0.0.1 (/playground)
error[E0596]: cannot borrow data in a `&` reference as mutable
--> src/main.rs:27:9
|
27 | self.user.as_ref().unwrap().set_name("Cake")
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot borrow as mutable
error: aborting due to previous error
For more information about this error, try `rustc --explain E0596`.
error: could not compile `playground`.
To learn more, run the command again with --verbose.
The whole code is on Rust Playground: Rust Playground
I thought I took a mutable self.user.as_ref()
via the &mut self
keyword, but why can't I borrow the reference as mutable?