In this following code playgroud.
The first solution doesn't compile:
let c = manager.add_currency(Currency {
name: "EUR".to_string(),
});
let _i = manager.add_instrument(Instrument {
name: "RRR".to_string(),
currency: c.clone(),
});
with this error msg
Compiling playground v0.0.1 (/playground)
error[E0499]: cannot borrow `manager` as mutable more than once at a time
--> src/main.rs:59:18
|
55 | let c = manager.add_currency(Currency {
| _________________-
56 | | name: "EUR".to_string(),
57 | | });
| |__________- first mutable borrow occurs here
58 |
59 | let _i = manager.add_instrument(Instrument {
| __________________^
60 | | name: "RRR".to_string(),
61 | | currency: c.clone(),
| | --------- first borrow later used here
62 | | });
| |__________^ second mutable borrow occurs here
For more information about this error, try `rustc --explain E0499`.
error: could not compile `playground` due to previous error
according to this, i understand "c" is an reference on Rc and borrow/mutable manager because add_currency is mutable. right ?
so add_instrument want to borrow/mutable manager, there is an error
But if i update my code :
manager.add_currency(Currency {
name: "EUR".to_string(),
});
let c = manager.get_last_currency();
let _i = manager.add_instrument(Instrument {
name: "RRR".to_string(),
currency: c.clone(),
});
compile without error it is same situation c is an borrow/not mutable from manager and it is impossible with a variable to borrow as mutable and un mutable in same time.
Where am I wrong ?