Rust errors / Ownership and borrowing
Rust compiler error · checked with rustc 1.99.0
Rust error E0713: borrow may still be in use when destructor runs
What it meansThis error occurs when an attempt is made to borrow state past the end of the lifetime of a type that implements the Drop trait.
What rustc prints
error[E0713]: borrow may still be in use when destructor runs
--> main.rs:7:51
|
7 | fn demo<'a>(s: S<'a>) -> &'a mut String { let p = &mut *s.data; p }
| -- ^^^^^^^^^^^^ - - here, drop of `s` needs exclusive access to `*s.data`, because the type `S<'_>` implements the `Drop` trait
| | |
| lifetime `'a` defined here returning this value requires that `*s.data` is borrowed for `'a`Real output of rustc 1.99.0 compiling the official example below (2021 edition). Your names and line numbers will differ; the first line is the part to match.
The compiler team's explanation
This error occurs when an attempt is made to borrow state past the end of the lifetime of a type that implements the Drop trait.
Erroneous code example:
pub struct S<'a> { data: &'a mut String }
impl<'a> Drop for S<'a> {
fn drop(&mut self) { self.data.push_str("being dropped"); }
}
fn demo<'a>(s: S<'a>) -> &'a mut String { let p = &mut *s.data; p }
Here, demo tries to borrow the string data held within its argument s and then return that borrow. However, S is declared as implementing Drop.
Structs implementing the Drop trait have an implicit destructor that gets called when they go out of scope. This destructor gets exclusive access to the fields of the struct when it runs.
This means that when s reaches the end of demo, its destructor gets exclusive access to its &mut-borrowed string data. allowing another borrow of that string data (p), to exist across the drop of s would be a violation of the principle that &mut-borrows have exclusive, unaliased access to their referenced data.
This error can be fixed by changing demo so that the destructor does not run while the string-data is borrowed; for example by taking S by reference:
pub struct S<'a> { data: &'a mut String }
impl<'a> Drop for S<'a> {
fn drop(&mut self) { self.data.push_str("being dropped"); }
}
fn demo<'a>(s: &'a mut S<'a>) -> &'a mut String { let p = &mut *(*s).data; p }
Note that this approach needs a reference to S with lifetime 'a. Nothing shorter than 'a will suffice: a shorter lifetime would imply that after demo finishes executing, something else (such as the destructor!) could access s.data after the end of that shorter lifetime, which would again violate the &mut-borrow's exclusive access.
From rustc --explain E0713 (1.99.0), part of the Rust project, MIT or Apache-2.0. Official page.
Related errors
- E0382borrow of moved value
- E0499cannot borrow as mutable more than once at a time
- E0502cannot borrow as mutable because it is also borrowed as immutable
- E0106missing lifetime specifier
- E0700hidden type for `impl Trait<'y>` captures lifetime that does not appear in bounds
- E0712thread-local variable borrowed past end of function
- E0716temporary value dropped while borrowed
- E0726implicit elided lifetime not allowed here
Up next
Often hit nextE0382: borrow of moved valueA related error from the same family.Read it next →Did this answer your question?