Rust
E0713 Borrow in Use When Destructor Runs Fix
A value that implements Drop cannot hand out a borrow that outlives it: when the value goes out of scope, its destructor gets exclusive access to the very data the borrow points at. This guide works the shapes that produce E0713 - a wrapper taken by value that tries to return the reference it holds, in a free function, a method, a generic, a nested wrapper, a two-field wrapper, and a wrapper over a vector - and matches each one to its repair: take or clone an owned value out, or keep the borrow inside the call so nothing has to outlive the destructor.
Written November 29, 2026 · Tested with Rust 1.99.1 · 33 min read
- The rule underneath: a destructor is a hidden last use
- How to read the diagnostic
- The first failure: the index's wrapper, taken by value, returning its borrow
- The second failure: a method that consumes self
Written November 29, 2026
Tested with Rust 1.99.1
What this guide is
Error E0713 is a borrow that may still be in use when a destructor runs. The error codes index opens with exactly that framing: "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." Its example is the shape this guide starts from: a struct S<'a> holding data: &'a mut String, an impl Drop for S whose drop writes into that string, and a function demo that takes s: S<'a> by value and tries to return &'a mut String borrowed from s.data. The program is rejected before it runs - when demo returns, s goes out of scope, its destructor runs, and the destructor is entitled to exclusive access to *s.data, the very string the returned borrow still points at.
Three conditions decide whether this error is the one that appears, and they are worth stating before any of the failures. First, the type implements Drop - the identical wrapper with no Drop impl compiles, and that control case is worked in the traps. Second, the wrapper is consumed - taken by value, as a parameter or as a self receiver - so its destructor is scheduled at the end of the function that received it. Third, a mutable borrow of the data behind one of its reference fields is still live at that point, because the function returns it. Change any one of the three and the error changes or disappears: return a shared borrow instead of a mutable one and the first failure's shape compiles as it stands; keep the borrow inside the function and return a plain value and it compiles; take the wrapper by reference and the borrow never has to cross a destructor at all - though, as the traps show, calling that form has its own sharp edges.
The forms in this guide each reproduce E0713 with a small, complete program: the index's own by-value wrapper, a method that consumes self, a generic wrapper over &mut T, a wrapper nested inside a second Drop type, a two-field wrapper returning the field its destructor does not touch, and a wrapper over a &mut Vec returning one element. Each failure is followed by its repair. The repairs share one idea - nothing borrowed may cross the destructor - and differ in how they honour it: move an owned value out of the wrapper before it drops, clone the value out, or never let the borrow leave the call in the first place.
Build every program in this guide with rustc --edition 2021. In the compiler transcripts below, the file names are the bare names shown on the command line.
The rule underneath: a destructor is a hidden last use
A Drop implementation is code that runs at a specific moment - when its value goes out of scope - and it runs with &mut self, which means it may touch every field the value holds. The borrow rules in References and Borrowing allow one mutable reference to a value, never two; a borrow returned from a function is still live after the function returns, so if the function's own parameter is dropped at that same boundary, the destructor's access and the returned borrow would be two holders of unique access to the same data. The compiler therefore treats the drop of a Drop value as a use of everything the value holds, scheduled at the end of its scope, and rejects any borrow that would still be in use at that moment.
That is also why the error is about the wrapper's lifetime, not about the data's. The String behind s.data in the first failure outlives demo - it belongs to the caller, and the returned &'a mut String names its real lifetime correctly. What does not outlive demo is s itself, and the borrow is routed through s: the returned reference is produced from a field of a value whose destructor is about to claim that field. Every repair in this guide works on that routing. Return something owned - taken out of the string with std::mem::take, or cloned - and the result no longer passes through the wrapper at all. Keep the borrow inside the call, as the callback repair does, and it ends before the destructor runs. Or remove a condition: drop the Drop impl, or return a shared borrow, and the conflict the code names is simply not present.
How to read the diagnostic
The header states the conflict in one line: "borrow may still be in use when destructor runs". The spans then name the two ends of it. The caret span sits under the returned expression - p, self.data, g.data, p.second - and carries two labels: "returning this value requires that *s.data is borrowed for 'a", which is the borrow that must stay live, and "here, drop of s needs exclusive access to *s.data, because the type S<'_> implements the Drop trait", which is the scheduled use it collides with. A short span on the parameter, labeled "lifetime 'a defined here", completes the picture. Read those two labels together and the repair is already chosen in outline: either the returned value stops being a borrow of *s.data, or s stops being dropped while the borrow lives.
That reading also separates this error from its neighbors before any repair starts. If the struct declares its reference field without a lifetime at all, the header is E0106, "missing lifetime specifier", and it fires at the declaration, before any function body is checked. If the function returns a reference to a value it created locally, the header is E0515, "cannot return value referencing function parameter" or "cannot return reference to local variable". If the wrapper is taken by reference - the error index's own suggested shape - but the caller then drops the wrapper or reads through it while the returned borrow's lifetime is still open, the header is E0597, "does not live long enough", or E0502; that caller-side failure is worked in the traps. And if there is no Drop impl, or the borrow is shared, or nothing borrowed escapes, there is no error at all - all three controls are worked in the traps.
The first failure: the index's wrapper, taken by value, returning its borrow
This is the error index's own example, given a caller. S wraps &'a mut String and implements Drop; demo takes s: S<'a> by value and returns a reborrow of the string behind it:
pub struct S<'a> { data: &'a mut String }
impl<'a> Drop for S<'a> {
fn drop(&mut self) { self.data.push_str(" [dropped]"); }
}
fn demo<'a>(s: S<'a>) -> &'a mut String { let p = &mut *s.data; p }
fn main() { let mut text = String::from("hi"); let s = S { data: &mut text }; let out = demo(s); println!("out is {out}"); }
error[E0713]: borrow may still be in use when destructor runs
--> a1.rs:5:51
|
5 | 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`
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0713`.
The span under p carries the whole story: returning p requires *s.data to be borrowed for 'a, and the drop of s at the end of demo needs exclusive access to that same *s.data. The first repair returns an owned value instead. std::mem::take moves the string's contents out through the reference, leaving an empty string in its place; what crosses the function's boundary is a String the caller owns, not a borrow, so the destructor - which then runs on a wrapper whose string is empty - has nothing to conflict with:
pub struct S<'a> { data: &'a mut String }
impl<'a> Drop for S<'a> {
fn drop(&mut self) { self.data.push_str("[dropped]"); }
}
fn demo(s: S) -> String { std::mem::take(&mut *s.data) }
fn main() { let mut text = String::from("hi"); let s = S { data: &mut text }; let out = demo(s); println!("out is {out}"); println!("text is {text}"); }
out is hi
text is [dropped]
The output is out is hi, then text is [dropped]: the caller gets the original contents, and the caller's own string - emptied by the take - receives only the destructor's marker when s drops at the end of demo. The trade is the take itself: the value behind the reference is consumed, and the caller's string is left empty. Where the caller still needs the string intact afterward, the second repair keeps the borrow but never lets it escape - the function takes the wrapper by value together with a callback, lends the callback the mutable borrow for the duration of the call, and returns nothing borrowed:
pub struct S<'a> { data: &'a mut String }
impl<'a> Drop for S<'a> {
fn drop(&mut self) { self.data.push_str(" [dropped]"); }
}
fn demo<F: FnOnce(&mut String)>(s: S, f: F) { let p = &mut *s.data; f(p); }
fn main() {
let mut text = String::from("hi");
{
let s = S { data: &mut text };
demo(s, |d| { d.push_str("!"); println!("inside is {d}"); });
}
println!("text is {text}");
}
inside is hi!
text is hi! [dropped]
The output is inside is hi!, then text is hi! [dropped]: the callback's edit lands, the wrapper drops at the end of demo with no borrow live, and the caller reads the edited string afterward. Between the two, the choice is whether the caller needs the data after the call as a value it owns (take it) or only needs work done on it in place (lend it to a callback).
The second failure: a method that consumes self
The by-value parameter does not have to be a function parameter. A method that takes self by value schedules the same destructor at the same boundary, and returning the field's borrow from it draws the same error:
pub struct S<'a> { data: &'a mut String }
impl<'a> Drop for S<'a> {
fn drop(&mut self) { self.data.push_str(" [dropped]"); }
}
impl<'a> S<'a> {
fn take_ref(self) -> &'a mut String { self.data }
}
fn main() { let mut text = String::from("hi"); let s = S { data: &mut text }; let out = s.take_ref(); println!("out is {out}"); }
error[E0713]: borrow may still be in use when destructor runs
--> a3.rs:6:43
|
5 | impl<'a> S<'a> {
| -- lifetime `'a` defined here
6 | fn take_ref(self) -> &'a mut String { self.data }
| ^^^^^^^^^ - here, drop of `self` needs exclusive access to `*self.data`, because the type `S<'_>` implements the `Drop` trait
| |
| returning this value requires that `*self.data` is borrowed for `'a`
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0713`.
The labels track the change of shape exactly: the value whose drop needs exclusive access is now self, and the field is *self.data. The repair is the first failure's take, expressed as the method's consuming form - an into_ method that moves the contents out and returns them owned:
pub struct S<'a> { data: &'a mut String }
impl<'a> Drop for S<'a> {
fn drop(&mut self) { self.data.push_str(" [dropped]"); }
}
impl<'a> S<'a> {
fn into_string(self) -> String { std::mem::take(&mut *self.data) }
}
fn main() { let mut text = String::from("hi"); let s = S { data: &mut text }; let out = s.into_string(); println!("out is {out}"); println!("text is {text}"); }
out is hi
text is [dropped]
The output is out is hi, then text is [dropped]: the method returns the contents, and the emptied caller's string shows the two spaces where the taken hi used to sit in front of the destructor's marker. A consuming method is the natural home for this repair - the method's name already tells the caller the wrapper is gone and what it gets back is a value, not a view into one.
The third failure: a generic wrapper over &mut T
Nothing about the conflict is specific to String. A wrapper generic over the borrowed type - G<'a, T> holding &'a mut T, with a Drop impl that does nothing at all - draws the same error when a function takes it by value and returns the borrow:
pub struct G<'a, T> { data: &'a mut T }
impl<'a, T> Drop for G<'a, T> {
fn drop(&mut self) {}
}
fn demo<'a, T>(g: G<'a, T>) -> &'a mut T { g.data }
fn main() { let mut n = 41; let g = G { data: &mut n }; let out = demo(g); *out += 1; println!("out is {out}"); }
error[E0713]: borrow may still be in use when destructor runs
--> a4.rs:5:44
|
5 | fn demo<'a, T>(g: G<'a, T>) -> &'a mut T { g.data }
| -- ^^^^^^ - here, drop of `g` needs exclusive access to `*g.data`, because the type `G<'_, T>` implements the `Drop` trait
| | |
| lifetime `'a` defined here returning this value requires that `*g.data` is borrowed for `'a`
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0713`.
An empty destructor is still a destructor: the compiler cannot see that this drop touches nothing - a Drop impl is entitled to &mut self access to the whole value, and that entitlement is what the returned borrow collides with. The repair, for a generic, is to ask the caller for the one capability that lets a value leave without a borrow: Clone. With a T: Clone bound, the function clones the value out through the reference and returns the clone owned:
pub struct G<'a, T> { data: &'a mut T }
impl<'a, T> Drop for G<'a, T> {
fn drop(&mut self) {}
}
fn demo<T: Clone>(g: G<T>) -> T { g.data.clone() }
fn main() { let mut n = 41; let g = G { data: &mut n }; let out = demo(g); println!("out is {out}"); println!("n is {n}"); }
out is 41
n is 41
The output is out is 41, then n is 41: the clone carries the value as it was, and the caller's variable is untouched - no take happened, so nothing was emptied. Cloning is the repair to reach for when the wrapper must keep its data and the type can afford the copy; where it cannot, the callback form from the first failure lends the borrow inside the call instead, at no bound on T.
The fourth failure: a Drop wrapper nested inside a second Drop type
Nesting does not dilute the conflict - it names the outer value in it. Here Outer holds an S, both types implement Drop, and demo takes the Outer by value and returns the borrow from the inner wrapper's field:
pub struct S<'a> { data: &'a mut String }
impl<'a> Drop for S<'a> {
fn drop(&mut self) { self.data.push_str(" [inner dropped]"); }
}
pub struct Outer<'a> { inner: S<'a> }
impl<'a> Drop for Outer<'a> {
fn drop(&mut self) { self.inner.data.push_str(" [outer dropped]"); }
}
fn demo<'a>(o: Outer<'a>) -> &'a mut String { o.inner.data }
fn main() { let mut text = String::from("hi"); let o = Outer { inner: S { data: &mut text } }; let out = demo(o); println!("out is {out}"); }
error[E0713]: borrow may still be in use when destructor runs
--> a5.rs:9:47
|
9 | fn demo<'a>(o: Outer<'a>) -> &'a mut String { o.inner.data }
| -- ^^^^^^^^^^^^ - here, drop of `o` needs exclusive access to `*o.inner.data`, because the type `Outer<'_>` implements the `Drop` trait
| | |
| lifetime `'a` defined here returning this value requires that `*o.inner.data` is borrowed for `'a`
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0713`.
The diagnostic follows the borrow's path: the returned expression is o.inner.data, the value whose drop needs exclusive access is o, and the place named is *o.inner.data. The outer destructor runs first when o goes out of scope, and it is entitled to the whole value, inner wrapper included. The repair is the take again, routed through the nesting - move the contents out of the inner field before either destructor runs, and return them owned:
pub struct S<'a> { data: &'a mut String }
impl<'a> Drop for S<'a> {
fn drop(&mut self) { self.data.push_str(" [inner dropped]"); }
}
pub struct Outer<'a> { inner: S<'a> }
impl<'a> Drop for Outer<'a> {
fn drop(&mut self) { self.inner.data.push_str(" [outer dropped]"); }
}
fn demo(o: Outer) -> String { std::mem::take(&mut *o.inner.data) }
fn main() { let mut text = String::from("hi"); let o = Outer { inner: S { data: &mut text } }; let out = demo(o); println!("out is {out}"); println!("text is {text}"); }
out is hi
text is [outer dropped] [inner dropped]
The output is out is hi, then text is [outer dropped] [inner dropped]: both destructors still run, in their usual order - the outer's drop body first, then the inner's as the field is dropped - and both write into the emptied string the take left behind. The borrow is gone by then, so neither has anything to conflict with.
The fifth failure: returning the field the destructor does not touch
A destructor that visibly uses only one field does not free the others. Pair holds two &mut String fields; its drop writes only into first. Returning second - the field the destructor never names - still draws the error:
pub struct Pair<'a> { first: &'a mut String, second: &'a mut String }
impl<'a> Drop for Pair<'a> {
fn drop(&mut self) { self.first.push_str(" [dropped]"); }
}
fn demo<'a>(p: Pair<'a>) -> &'a mut String { p.second }
fn main() { let mut a = String::from("a"); let mut b = String::from("b"); let p = Pair { first: &mut a, second: &mut b }; let out = demo(p); println!("out is {out}"); }
error[E0713]: borrow may still be in use when destructor runs
--> a6.rs:5:46
|
5 | fn demo<'a>(p: Pair<'a>) -> &'a mut String { p.second }
| -- ^^^^^^^^ - here, drop of `p` needs exclusive access to `*p.second`, because the type `Pair<'_>` implements the `Drop` trait
| | |
| lifetime `'a` defined here returning this value requires that `*p.second` is borrowed for `'a`
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0713`.
The label says why: "drop of p needs exclusive access to *p.second, because the type Pair<'_> implements the Drop trait". drop receives &mut self - the whole value, every field - whatever its body happens to touch today, and a later edit to the body could touch the rest. The compiler checks the entitlement, not the current body. The repair takes the returned field's contents out owned, exactly as in the first failure:
pub struct Pair<'a> { first: &'a mut String, second: &'a mut String }
impl<'a> Drop for Pair<'a> {
fn drop(&mut self) { self.first.push_str(" [dropped]"); }
}
fn demo(p: Pair) -> String { std::mem::take(&mut *p.second) }
fn main() { let mut a = String::from("a"); let mut b = String::from("b"); let p = Pair { first: &mut a, second: &mut b }; let out = demo(p); println!("out is {out}"); println!("a is {a}"); println!("b is {b}"); }
out is b
a is a [dropped]
b is
The output is out is b, then a is a [dropped], then b is: the second string's contents leave with the caller, the first string receives the destructor's marker when p drops, and the second is left empty by the take. Splitting a wrapper's fields between "returned" and "dropped" is not a distinction the borrow checker makes - plan on returning owned values for any field of a Drop type taken by value.
The sixth failure: returning one element of a borrowed Vec
The borrow that escapes does not have to be the field itself - a borrow of anything behind the field is enough. S wraps &'a mut Vec<String>; demo takes it by value and returns a mutable borrow of the vector's first element:
pub struct S<'a> { data: &'a mut Vec<String> }
impl<'a> Drop for S<'a> {
fn drop(&mut self) { self.data.push(String::from("dropped")); }
}
fn demo<'a>(s: S<'a>) -> &'a mut String { &mut s.data[0] }
fn main() { let mut v = vec![String::from("hi")]; let s = S { data: &mut v }; let out = demo(s); println!("out is {out}"); }
error[E0713]: borrow may still be in use when destructor runs
--> a8.rs:5:48
|
5 | fn demo<'a>(s: S<'a>) -> &'a mut String { &mut s.data[0] }
| -- -----^^^^^^--- - 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`
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0713`.
The span under &mut s.data[0] names *s.data - the whole place behind the reference, not the element - because the destructor's entitlement is to the vector, and the element borrow lives inside it. The repair clones the element out. A single element is usually cheap to copy, and the clone severs the result from the vector entirely:
pub struct S<'a> { data: &'a mut Vec<String> }
impl<'a> Drop for S<'a> {
fn drop(&mut self) { self.data.push(String::from("dropped")); }
}
fn demo(s: S) -> String { s.data[0].clone() }
fn main() { let mut v = vec![String::from("hi")]; let s = S { data: &mut v }; let out = demo(s); println!("out is {out}"); println!("v is {v:?}"); }
out is hi
v is ["hi", "dropped"]
The output is out is hi, then v is ["hi", "dropped"]: the caller's vector keeps its element, gains the destructor's pushed marker when s drops at the end of demo, and the caller holds an independent copy of the element. Where the element type is expensive or not Clone, the callback form is the fallback - lend &mut s.data[0] to a closure inside the function, and let the borrow end before the wrapper drops.
The fixes in reach order
- Return an owned value taken out of the wrapper -
std::mem::takethrough the reference field - the repair for the first, second, fourth, and fifth failures. The caller's value behind the reference is left emptied, and the destructor runs on the wrapper with nothing borrowed outstanding. - Clone the value out - the repair for the third and sixth failures. The caller's data stays intact; the cost is the copy, and for a generic it costs a
T: Clonebound as well. - Keep the borrow inside the call - pass a callback and lend it the borrow - the alternative repair for the first failure, and the fallback wherever taking or cloning does not fit. Nothing borrowed crosses the function's boundary, so the destructor's schedule stops mattering.
- Change a condition instead: return a shared borrow where shared access is genuinely all the caller needs, or remove the
Dropimpl where the type does not need one - both compile as they stand, as the traps show. Neither is a way to keep a mutable borrow alive past the drop; they work by stepping outside the error's conditions, so reach for them only when the condition really is absent.
Traps
Drop value's scope ends. Here a borrow of s.data is stored in a variable declared outside the block, and s drops at the block's end:pub struct S<'a> { data: &'a mut String }
impl<'a> Drop for S<'a> {
fn drop(&mut self) { self.data.push_str(" [dropped]"); }
}
fn main() {
let mut text = String::from("hi");
let r;
{
let s = S { data: &mut text };
r = &*s.data;
}
println!("r is {r}");
}
error[E0713]: borrow may still be in use when destructor runs
--> n3.rs:10:13
|
10 | r = &*s.data;
| ^^^^^^^^
11 | }
| - here, drop of `s` needs exclusive access to `*s.data`, because the type `S<'_>` implements the `Drop` trait
12 | println!("r is {r}");
| - borrow later used here
|
= note: consider using a `let` binding to create a longer lived value
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0713`.
The shape of the diagnostic changes with the shape of the program - the span sits on the assignment, the drop is located at the block's closing brace, and the later use of r is named - but the code and the labels are this guide's. The repair is to keep the borrow's uses inside the scope: read the value while s is alive, and let the block end with nothing borrowed outstanding:
pub struct S<'a> { data: &'a mut String }
impl<'a> Drop for S<'a> {
fn drop(&mut self) { self.data.push_str(" [dropped]"); }
}
fn main() {
let mut text = String::from("hi");
{
let s = S { data: &mut text };
let r = &*s.data;
println!("r is {r}");
}
println!("text is {text}");
}
r is hi
text is hi [dropped]
The output is r is hi, then text is hi [dropped]. Any pattern that parks a borrow of a Drop wrapper's field somewhere longer-lived than the wrapper - a variable outside the block, a collection, a struct field - is this trap in another outfit.
Routing the borrow through a helper changes nothing - still E0713. Passing the reborrow through a second function on its way out does not launder it:
pub struct S<'a> { data: &'a mut String }
impl<'a> Drop for S<'a> {
fn drop(&mut self) { self.data.push_str(" [dropped]"); }
}
fn helper<'a>(x: &'a mut String) -> &'a mut String { x }
fn demo<'a>(s: S<'a>) -> &'a mut String { let p = &mut *s.data; helper(p) }
fn main() { let mut text = String::from("hi"); let s = S { data: &mut text }; let out = demo(s); println!("out is {out}"); }
error[E0713]: borrow may still be in use when destructor runs
--> a7.rs:6:51
|
6 | fn demo<'a>(s: S<'a>) -> &'a mut String { let p = &mut *s.data; helper(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`
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0713`.
The error is reported at demo's return expression, with the helper's call visible in the span - the borrow's provenance is what matters, not the number of hands it passed through. The repairs are the first failure's: return an owned value, or keep the borrow inside the call.
A shared borrow compiles. Make the returned borrow shared - the wrapper holds &'a String, demo returns &'a String - and the first failure's shape builds and runs as it stands, destructor and all:
pub struct S<'a> { data: &'a String }
impl<'a> Drop for S<'a> {
fn drop(&mut self) { println!("dropping S, data was {}", self.data); }
}
fn demo<'a>(s: S<'a>) -> &'a String { let p = &*s.data; p }
fn main() { let text = String::from("hi"); let s = S { data: &text }; let out = demo(s); println!("out is {out}"); }
dropping S, data was hi
out is hi
The output order shows the mechanics: dropping S, data was hi prints first, from inside demo as s drops on return, and only then does the caller print the still-valid shared borrow. A shared borrow does not promise exclusive access, so the destructor's own access does not collide with it in the way this error polices. The limit is real, though: the caller gets a read-only view. If the caller needs to mutate through the result, this is not a repair - it is a different function.
No Drop impl - compiles. Remove the Drop impl from the first failure's wrapper and the identical by-value function, returning the identical mutable borrow, builds and runs:
pub struct ND<'a> { data: &'a mut String }
fn demo<'a>(nd: ND<'a>) -> &'a mut String { let p = &mut *nd.data; p }
fn main() { let mut text = String::from("hi"); let nd = ND { data: &mut text }; let out = demo(nd); out.push_str("!"); println!("out is {out}"); }
out is hi!
The output is out is hi!. This is the control case that isolates the trigger: the by-value parameter, the reference field, and the returned borrow are all present, and only the destructor is missing. It cuts both ways as a diagnostic - if a program like this fails, the type (or a type wrapping it, as in the fourth failure) does implement Drop somewhere - and as a repair only where the Drop impl is genuinely unnecessary.
Returning a plain value compiles. A borrow that is used and finished inside the function, with only a value derived from it returned, never meets the destructor:
pub struct S<'a> { data: &'a mut String }
impl<'a> Drop for S<'a> {
fn drop(&mut self) { self.data.push_str(" [dropped]"); }
}
fn demo(s: S) -> usize { s.data.len() }
fn main() {
let mut text = String::from("hi");
let n;
{
let s = S { data: &mut text };
n = demo(s);
}
println!("len is {n}");
println!("text is {text}");
}
len is 2
text is hi [dropped]
The output is len is 2, then text is hi [dropped]. Lengths, counts, flags, and owned summaries of the borrowed data are all free to return; it is the borrow itself that cannot cross.
Taking the wrapper by reference, then dropping it too early - E0597, not a fix that failed. The error index's suggested shape for this error is to take S by reference - fn demo<'a>(s: &'a mut S<'a>) -> &'a mut String - so that no destructor is scheduled inside demo at all. That signature is sound, but it moves the obligation to the caller: the returned borrow's lifetime is the wrapper's own lifetime parameter, so the caller must keep the wrapper alive, and untouched, for as long as the borrow is in play. Scope the wrapper in a block that ends while the borrow is still held, and the error reappears under a different code:
pub struct S<'a> { data: &'a mut String }
impl<'a> Drop for S<'a> {
fn drop(&mut self) { self.data.push_str(" [dropped]"); }
}
fn demo<'a>(s: &'a mut S<'a>) -> &'a mut String { let p = &mut *(*s).data; p }
fn main() {
let mut text = String::from("hi");
{
let mut s = S { data: &mut text };
let out = demo(&mut s);
out.push_str("!");
println!("out is {out}");
}
println!("text is {text}");
}
error[E0597]: `s` does not live long enough
--> b3.rs:10:24
|
9 | let mut s = S { data: &mut text };
| ----- binding `s` declared here
10 | let out = demo(&mut s);
| ^^^^^^ borrowed value does not live long enough
...
13 | }
| -
| |
| `s` dropped here while still borrowed
| borrow might be used here, when `s` is dropped and runs the `Drop` code for type `S`
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0597`.
The header is E0597, "s does not live long enough", and its note names the same mechanism from the caller's side: "s dropped here while still borrowed - borrow might be used here, when s is dropped and runs the Drop code for type S". Read onward through the wrapper after the borrow, or move it with drop(s), and the same family answers with E0502 or E0505. The by-reference form is the right repair when the caller genuinely owns the wrapper for the borrow's whole life - typically when the wrapper itself lives in a longer-lived owner - and the owned-value and callback repairs above are the right ones when it does not.
Wrapping the wrapper in ManuallyDrop - E0515. ManuallyDrop<S> suppresses the destructor, so it looks like the condition-remover. In practice the borrow cannot be gotten out: a reborrow through the ManuallyDrop value is a borrow of the function parameter, not a borrow with lifetime 'a, and the compiler names it so:
use std::mem::ManuallyDrop;
pub struct S<'a> { data: &'a mut String }
impl<'a> Drop for S<'a> {
fn drop(&mut self) { self.data.push_str(" [dropped]"); }
}
fn demo<'a>(mut s: ManuallyDrop<S<'a>>) -> &'a mut String { let p = &mut *s.data; p }
fn main() { let mut text = String::from("hi"); let s = ManuallyDrop::new(S { data: &mut text }); let out = demo(s); out.push_str("!"); println!("out is {out}"); println!("text is {text}"); }
error[E0515]: cannot return value referencing function parameter `s`
--> r3.rs:6:83
|
6 | fn demo<'a>(mut s: ManuallyDrop<S<'a>>) -> &'a mut String { let p = &mut *s.data; p }
| - ^ returns a value referencing data owned by the current function
| |
| `s` is borrowed here
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0515`.
The header is E0515, "cannot return value referencing function parameter s". Extracting the reference from a ManuallyDrop without reborrowing through the local takes unsafe code; the owned-value repairs get the same result without it.
A reference to a local - E0515 again, different defect. A function that creates a value and returns a reference to it fails with the same neighboring code and none of this guide's conditions - there is no Drop wrapper and no reference field:
fn foo<'a>() -> &'a String { let s = String::from("hi"); &s }
fn main() { let out = foo(); println!("out is {out}"); }
error[E0515]: cannot return reference to local variable `s`
--> n4.rs:1:58
|
1 | fn foo<'a>() -> &'a String { let s = String::from("hi"); &s }
| ^^ returns a reference to data owned by the current function
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0515`.
The tell is in the labels: E0515's span says the returned value references data owned by the current function - a local with no lifetime parameter to borrow from - where E0713's spans name a drop needing exclusive access to a field. If the diagnostic does not mention a destructor, this guide's repairs are not the ones to apply; return the owned value.
No lifetime on the field - E0106. Declare the wrapper's reference field without a lifetime parameter and the program fails earlier than any of this guide's failures - at the declaration itself:
pub struct S { data: &mut String }
impl Drop for S {
fn drop(&mut self) { self.data.push_str(" [dropped]"); }
}
fn demo(s: S) -> &mut String { &mut *s.data }
fn main() { let mut text = String::from("hi"); let s = S { data: &mut text }; let out = demo(s); println!("out is {out}"); }
error[E0106]: missing lifetime specifier
--> n2.rs:1:22
|
1 | pub struct S { data: &mut String }
| ^ expected named lifetime parameter
|
help: consider introducing a named lifetime parameter
|
1 | pub struct S<'a> { data: &'a mut String }
| ++++ ++
error[E0106]: missing lifetime specifier
--> n2.rs:5:18
|
5 | fn demo(s: S) -> &mut String { &mut *s.data }
| ^ expected named lifetime parameter
|
= help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from
help: consider using the `'static` lifetime, but this is uncommon unless you're returning a borrowed value from a `const` or a `static`
|
5 | fn demo(s: S) -> &'static mut String { &mut *s.data }
| +++++++
help: instead, you are more likely to want to change the argument to be borrowed...
|
5 | fn demo(s: &S) -> &mut String { &mut *s.data }
| +
help: ...or alternatively, you might want to return an owned value
|
5 - fn demo(s: S) -> &mut String { &mut *s.data }
5 + fn demo(s: S) -> String { &mut *s.data }
|
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0106`.
Both errors are E0106, "missing lifetime specifier" - one on the struct, one on the function that then has no lifetime to name. The same code answers a bare fn foo() -> &String returning a reference with no source at all. Lifetimes are the vocabulary every signature in this guide is written in; a program that cannot state them yet is not at the destructor problem yet.
Decision summary
- The header is E0713 when three facts hold together: the value implements
Drop, it is consumed by value (a parameter or a consumingself), and a mutable borrow of data behind one of its reference fields is still live when its scope ends - most often because the function returns that borrow. - The spans are the two ends of the conflict: the returned expression, labeled with the borrow that must stay live for
'a, and the drop of the value, labeled as needing exclusive access to the same place because the type implementsDrop. - The destructor's entitlement is to the whole value: returning a field its
dropbody never touches, an element behind the field, or a borrow routed through a helper all draw the same error, and nesting the wrapper inside a secondDroptype names the outer value instead. - Neighbours ruled out by the header before applying this guide's repairs: E0106 when the lifetime itself is missing, E0515 when the returned reference points at a local or is reborrowed out of a
ManuallyDrop, E0597 when a by-reference wrapper is dropped or read while its returned borrow is still in play. - Not an error at all: the same shape with a shared borrow, the same shape with no
Dropimpl, and any shape where the borrow is used and finished inside the function and only a value comes back.
Checklist
- Find the three conditions: a
Dropimpl (on the type or on a type wrapping it), a by-value consumption, and a mutable borrow still live at the scope's end. If one is missing, the header will not be E0713 - check the traps for the code it will be. - Decide what the caller actually needs after the call: an owned value (take or clone it out), work done in place (lend the borrow to a callback), or a read-only view (a shared borrow may already compile).
- Do not reach for
ManuallyDropto suppress the destructor - the borrow cannot be reborrowed out of it withoutunsafe, and the attempt surfaces as E0515. - If you take the wrapper by reference instead, audit the caller: the wrapper must stay alive and untouched for the returned borrow's whole life, or the error returns as E0597, E0502, or E0505 at the call site.
- Do not soften the borrow to shared just to make the error go away unless reading is genuinely all the caller does - a shared borrow changes what the caller can do with the result, it does not preserve the function.
Related guides on this site
- E0626 Borrow Across Yield Fix
- E0501 Cannot Borrow as Immutable Fix
- E0040 Explicit Destructor Call Fix
- E0401 Can't Use Generic Parameters Fix
Sources
- Rust, Error codes index: E0713 - the definition of the error: an attempt to borrow state past the end of the lifetime of a type that implements the
Droptrait - with theS<'a>wrapper example this guide's first failure follows, and the by-reference shape whose caller-side obligations the traps work through. - The Rust Programming Language, The Drop Trait Runs Code on Cleanup - what a
Dropimplementation is and when it runs: automatically when a value goes out of scope, withdroptaking a mutable reference toself. - The Rust Programming Language, References and Borrowing - the borrow rules underneath: one mutable reference or any number of immutable references to a value, never both at once; and the scope rule the inside-the-block repair relies on - a reference's scope runs from where it is introduced through the last time it is used.
- Rust, Error codes index: E0106 - the neighboring error separated in the traps: a lifetime specifier is missing where one is required - the code for a reference field or return type declared with no lifetime at all.
- Rust, Error codes index: E0515 - the neighboring error separated in the traps: a value referencing a function parameter or a local variable is returned - the code for the
ManuallyDropreborrow and for returning a reference to a locally created value. - Rust, Error codes index: E0597 - the neighboring error separated in the traps: a value does not live long enough - the code a caller of the by-reference shape meets when the wrapper is dropped while its returned borrow is still in use.
Keep Exploring
The full Rust hub \u2014 ownership, structs and enums, async, and measured comparisons.
Every guide on the site in one list, grouped by topic.
Every browser-only checker and verifier on the site.
The C++, Python, Sysadmin, Rust, Go, and TypeScript hubs.