There are a couple of ways to interpret your question. In all the cases, the answer starts with "Your function signature is wrong", but what exactly it should be, depends on your usecase.
But most certainly, it should be one of:
pub fn add_account(&mut self, acct: Account)
pub fn add_account(&mut self, acct: Account) -> &mut Self
pub fn add_account(mut self, acct: Account) -> Self
I think the biggest misconception you are having is that you think you need to return Self
. The way you are using add_account
, by simply calling it on departments.add_account(Account::Credit)
does not require you to return Self
. You can directly modify the self
object:
pub struct Departments {
pub id: String,
pub linked_accounts: Vec<Account>,
}
impl Departments {
pub fn add_account(&mut self, acct: Account) {
self.linked_accounts.push(acct);
}
}
Now, there is a usecase where you want to return some form of Self
, and that is if you want to be able to chain those calls:
departments
.add_account(Account::Credit)
.add_account(Account::Debit)
.add_account(Account::Savings);
This is usually done by returning &mut Self
:
pub struct Departments {
pub id: String,
pub linked_accounts: Vec<Account>,
}
impl Departments {
pub fn add_account(&mut self, acct: Account) -> &mut Self {
self.linked_accounts.push(acct);
self
}
}
Note that this still doesn't require you to instantiate a new Self
object, like you were doing in your code.
The actual error
In case you want to understand the original error:
- The
&mut self
argument means that you only borrow self
, and you have to give it back at some point. (happens automatically at the end of the function)
let mut vec: Vec<Account> = self.linked_accounts;
moves the linked_accounts
member out of self
. This is only possible when we take ownership of self
instead of borrowing it, because then, we can dismantle it to our pleasing. But as we need to give it back, we cannot just move members out of it.
I didn't talk about the actual error in more detail, because it was just an artifact of the incorrect function signature and didn't really contribute meaningfully to a solution.
Either way, I think there are a couple of misunderstandings of yours as to how ownership works, so I recommend reading the ownership chapter of the Rust book.
The reason why I think you have misunderstandings, is because it is literally impossible to return Self
if your function signature contains &mut self
. Self
is owned, and you cannot create an owned object from a borrowed one without copying it.
Yes, you are trying to create a copy of it in your attempt to implement it, but the usage example you posted shows me that you don't actually want to create a copy.