If I have this:
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Balance {
#[serde(default)]
pub amount: f64,
#[serde(default)]
pub currency: Currency,
}
struct MyLifeTime<'a> {
part: &'a mut Balance,
balance: Balance,
}
impl<'a> MyLifeTime<'a> {
pub fn add_balance(&mut self, num: f64) {
self.part.amount = num;
self.balance.amount = num;
}
}
This runable:
#[test]
fn test_lft() {
let mut b = Balance {
amount: 1.1,
currency: Currency::SANDBOX,
};
let m = MyLifeTime {
balance: b.clone(),
part: &mut b,
};
}
But this doesn't:
#[test]
fn test_lft() {
let mut b = Balance {
amount: 1.1,
currency: Currency::SANDBOX,
};
let m = MyLifeTime {
part: &mut b,
balance: b.clone(),
};
}
|
265 | let m = MyLifeTime {
| _________________-
266 | | part: &mut b,
| | ------ mutable borrow occurs here
267 | | balance: b.clone(),
| | ^ immutable borrow occurs here
268 | | };
| |_________- mutable borrow later used here
The only difference is the two fields code before or after.
Why? I think the balance filed is clone(), but why said borrow occurs?