# Rust: error[E0716]: temporary value dropped while borrowed — creates a temporary value which is freed while still in use

**Code:** `E0716: temporary value dropped while borrowed` · **Category:** Rust · **Seen on:** Rust

A reference points to a temporary that's dropped at the end of the statement.

## Meaning

Bind the temporary to a variable so it lives longer: `let s = String::from("x"); let t = s.trim();`. Common with `.to_string().as_str()`, `format!(..).as_str()`, `&String::new()` in struct fields, or `lock().unwrap().get(...)` references.

## Common causes

- `foo(&String::from("x"))` result kept
- `.to_string().as_str()` stored
- `format!(...).trim()` stored in a variable
- Mutex guard temporary
- Iterator over a temporary collection

## Quick fix

1. Store the owner in a `let` first
2. Return owned data
3. Avoid chaining borrow after a temporary when keeping the result

## Detailed fix by platform

### Rust

1. `let owned = format!("{a}-{b}"); let part = owned.trim();`

## How to diagnose

1. **Temporary** — Which expression?
2. **Lifetime** — Needed longer?
3. **Fix** — let binding?

---
Source: [WhatIsMyError](https://whatismyerror.com/errors/rust/e0716-temporary-value-dropped-while-borrowed)
