I need a help to solve a borrowing problem.
The code in the link above is not practical, it's just a abstracted version of a code which I'm writing.
If you compile the code above, it will emit an error like this:
error[E0506]: cannot assign to `*data` because it is borrowed --> src/main.rs:15:9 | 10 | fn foo<'a, F>(data: &'a mut Data, f: F) | -- lifetime `'a` defined here ... 15 | *data = Data(i); | ^^^^^^^^^^^^^^^ assignment to borrowed `*data` occurs here 16 | let condition = f(data); | ------- | | | | | borrow of `*data` occurs here | argument requires that `*data` is borrowed for `'a` error[E0499]: cannot borrow `*data` as mutable more than once at a time --> src/main.rs:20:17 | 10 | fn foo<'a, F>(data: &'a mut Data, f: F) | -- lifetime `'a` defined here ... 16 | let condition = f(data); | ------- | | | | | first mutable borrow occurs here | argument requires that `*data` is borrowed for `'a` ... 20 | i = data.generate_new(); | ^^^^ second mutable borrow occurs here
Compiler says the
data is borrowed because of calling a function with it, but the function
f just returns a
bool value, not reference of anything. Therefore I think the borrowing of
data should be ended when the returned
bool value has assigned to a local variable and line 15 or 20 should be fine.
It would be grateful if someone explain what is going on and what I should do to avoid this error.