I have a struct and respective implementation:
pub struct Departments {
pub id: String,
pub linked_accounts: Vec<Account>,
}
impl Departments {
pub fn add_account(&mut self, acct: Account) -> Self {
let mut vec: Vec<Account> = self.linked_accounts;
vec.push(acct);
Self {
id: self.id,
linked_accounts: vec,
}
}
}
error[E0507]: cannot move out of `self.linked_accounts` which is behind a mutable reference
--> src/lib.rs:10:37
|
10 | let mut vec: Vec<Account> = self.linked_accounts;
| ^^^^^^^^^^^^^^^^^^^^ move occurs because `self.linked_accounts` has type `Vec<Account>`, which does not implement the `Copy` trait
|
help: consider borrowing here
|
10 | let mut vec: Vec<Account> = &self.linked_accounts;
| +
error[E0507]: cannot move out of `self.id` which is behind a mutable reference
--> src/lib.rs:14:17
|
14 | id: self.id, //*Error here*
| ^^^^^^^ move occurs because `self.id` has type `String`, which does not implement the `Copy` trait
I use it when loading the department from the DB, adding account, and storing it back to the DB:
match db.get::<Departments>(id).await? {
None => bail!("No records found"),
Some(departments) => {
let mut departments = departments;
departments.add_account(Account::Credit);
let id = Id::new(Departments::KIND, &id);
gh.update::<_, Departments>(id, departments.clone()).await?;
}
}
How can I fix this?
&mut selfand return an ownedSelf.