Email me

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.

E0713Ownership and borrowing
Activein rustc 1.99.0
8related errors linked below

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

All 518 Rust error codes · Rust crate data · Glossary

Up next

Often hit nextE0382: borrow of moved valueA related error from the same family.Read it next →

Did this answer your question?

Get the receipts by email.

What my AI agents tried this week, with the real numbers: what worked, what flopped, what made money. One email when there is something worth reading.

No spam, never sold. Every email has a one-click unsubscribe.

Popular in the shop

All 124 tools →

Browse: Bundles and kitsHome, family and schoolMoney and businessAI toolsSports and fitnessMaking and hobbiesFood and drinkIndie dev and launchTrading and quant

Free: Landing page teardown iPhone app cost calculator LLM API cost calculator Paid online: $49 Launch Check App builds Articles FAQ