Rust
E0623 Lifetime Mismatch Fix
A conversion trait promises to turn a reference with one lifetime into a reference with another, a wrapper type ties a value to both lifetimes, and the function that joins them declares the two lifetimes as strangers. E0623 is the compiler reporting that the parameter and the return type are declared with different lifetimes while data from the parameter is what gets returned: the fix is to state the relationship the code already assumes — one lifetime outlives the other — or to stop pretending there are two lifetimes and use one.
Written November 26, 2026 · Tested with Rust 1.99.1 · 8 min read
- The error: two lifetimes, no relationship stated
- The fix: add the outlives bound
- The fix: use one lifetime
- Which fix for which cause
Written November 26, 2026
Tested with Rust 1.99.1
What this guide is
Most lifetime errors name the expression that returns data for too short a time. E0623 is different: it is reported at the function's signature, because the mismatch is visible there before any single expression is blamed. A parameter and the return type are declared with different lifetimes, and — through a trait bound on a type the function takes — data from that parameter is exactly what the return value is built from. Two unrelated lifetimes cannot support that flow: the returned reference would claim a lifetime the input was never promised to have.
The trigger needs the lifetimes to be tied together indirectly. Returning one field of a struct as another field's lifetime, or returning a parameter under a second unrelated lifetime, draws the plainer "lifetime may not live long enough" report instead, because the compiler can point at the returning expression. E0623 appears when the connection runs through a bound — here, a Convert<'a, 'b> trait that converts a &'a reference into a &'b one, and a Linked<'a, 'b, T> wrapper whose where clause requires T: Convert<'a, 'b>. The function takes the wrapper and a &'in_ value and promises a &'out result, without ever saying how 'in_ and 'out relate. That missing relationship is the whole defect, and both repairs below supply it in a different way.
The error: two lifetimes, no relationship stated
This program declares the conversion trait, an implementation that can only convert from a longer lifetime to a shorter one, and a function that asks for a conversion between two lifetimes it leaves unrelated:
use std::marker::PhantomData;
struct Linked<'a, 'b, T>(PhantomData<(&'a (), &'b (), T)>)
where
T: Convert<'a, 'b>;
trait Convert<'a, 'b>: Sized {
fn cast(&'a self) -> &'b Self;
}
impl<'long: 'short, 'short, T> Convert<'long, 'short> for T {
fn cast(&'long self) -> &'short T {
self
}
}
fn convert_ref<'in_, 'out, T>(linked: Linked<'in_, 'out, T>, value: &'in_ T) -> &'out T {
let _ = &linked;
value.cast()
}
fn main() {
let text = String::from("hello");
let linked: Linked<'_, '_, String> = Linked(PhantomData);
println!("{}", convert_ref(linked, &text));
}
$ rustc --edition 2021 broken.rs -o broken.bin
error[E0623]: lifetime mismatch
--> broken.rs:17:39
|
17 | fn convert_ref<'in_, 'out, T>(linked: Linked<'in_, 'out, T>, value: &'in_ T) -> &'out T {
| ^^^^^^^^^^^^^^^^^^^^^ -------
| |
| this parameter and the return type are declared with different lifetimes...
| ...but data from `linked` is returned here
error: lifetime may not live long enough
--> broken.rs:19:5
|
17 | fn convert_ref<'in_, 'out, T>(linked: Linked<'in_, 'out, T>, value: &'in_ T) -> &'out T {
| ---- ---- lifetime `'out` defined here
| |
| lifetime `'in_` defined here
18 | let _ = &linked;
19 | value.cast()
| ^^^^^^^^^^^^ function was supposed to return data with lifetime `'out` but it is returning data with lifetime `'in_`
|
= help: consider adding the following bound: `'in_: 'out`
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0623`.
Read the two reports as one diagnosis at two levels. E0623 sits on the signature: the linked parameter is declared with <'in_, 'out> and the return type with 'out, and because Linked's bound makes T: Convert<'in_, 'out>, the value that comes back out of cast is data from that parameter. The second report sits on the body and says the same thing from the other side: value.cast() was supposed to return data with lifetime 'out and is returning data with lifetime 'in_. The only implementation of Convert converts from a longer-lived reference to a shorter-lived one — its own bound is 'long: 'short — so the conversion the function wants exists only if 'in_ outlives 'out. The signature never promises that, so a caller could pick an 'out that lasts longer than the data behind 'in_, and the returned reference would dangle. The compiler's help line names the missing promise directly: consider adding the bound 'in_: 'out.
The fix: add the outlives bound
Stating the relationship the body already assumes is the first repair. One bound added to the function's lifetime list — 'in_: 'out, read as "'in_ lives at least as long as 'out" — and nothing else changes:
use std::marker::PhantomData;
struct Linked<'a, 'b, T>(PhantomData<(&'a (), &'b (), T)>)
where
T: Convert<'a, 'b>;
trait Convert<'a, 'b>: Sized {
fn cast(&'a self) -> &'b Self;
}
impl<'long: 'short, 'short, T> Convert<'long, 'short> for T {
fn cast(&'long self) -> &'short T {
self
}
}
fn convert_ref<'in_: 'out, 'out, T>(linked: Linked<'in_, 'out, T>, value: &'in_ T) -> &'out T {
let _ = &linked;
value.cast()
}
fn main() {
let text = String::from("hello");
let linked: Linked<'_, '_, String> = Linked(PhantomData);
println!("{}", convert_ref(linked, &text));
}
$ rustc --edition 2021 fixed_bound.rs -o fixed_bound.bin
$ ./fixed_bound.bin
hello
The bound does not lengthen anything. Lifetime annotations describe relationships between references; they do not change how long any value lives. What the bound changes is what callers may ask for: a call where 'out would outlast 'in_ no longer type-checks, and every remaining call is one the conversion can honestly perform, because data that lives for 'in_ necessarily lives for the shorter-or-equal 'out. The function stays generic over two lifetimes, so callers that genuinely hold two different lifetimes keep working.
The fix: use one lifetime
When the two lifetimes were never meant to differ — when every real call passes references that live for the same span — the second repair is to say so. The function takes a single lifetime, uses it for the wrapper, the parameter, and the return type, and the question of a relationship disappears:
use std::marker::PhantomData;
struct Linked<'a, 'b, T>(PhantomData<(&'a (), &'b (), T)>)
where
T: Convert<'a, 'b>;
trait Convert<'a, 'b>: Sized {
fn cast(&'a self) -> &'b Self;
}
impl<'long: 'short, 'short, T> Convert<'long, 'short> for T {
fn cast(&'long self) -> &'short T {
self
}
}
fn convert_ref<'out, T>(linked: Linked<'out, 'out, T>, value: &'out T) -> &'out T {
let _ = &linked;
value.cast()
}
fn main() {
let text = String::from("hello");
let linked: Linked<'_, '_, String> = Linked(PhantomData);
println!("{}", convert_ref(linked, &text));
}
$ rustc --edition 2021 fixed_single.rs -o fixed_single.bin
$ ./fixed_single.bin
hello
With Linked<'out, 'out, T>, the bound the wrapper demands is T: Convert<'out, 'out>, and converting a reference to the same lifetime is the case the implementation covers trivially — a lifetime always outlives itself. This version is simpler and stricter at once: simpler because there is no bound to read, stricter because a caller holding two genuinely different lifetimes can no longer use it. Choose it when the second lifetime was speculative generality; choose the bound when the two-lifetime calls are real.
Which fix for which cause
| Cause | Message | Fix |
|---|---|---|
| Two lifetimes, conversion between them is real | E0623, this parameter and the return type are declared with different lifetimes | Add the outlives bound: 'in_: 'out |
| Two lifetimes declared, but every call uses one span | E0623, this parameter and the return type are declared with different lifetimes | Collapse to a single lifetime used for parameter and return |
Checklist
- Read where the error points. E0623 underlines the signature, not a body expression: the defect is a relationship the signature failed to state, not a value returned from the wrong place.
- Find the bound that ties the lifetimes together. A
whereclause or trait bound connecting the parameter's type to the return lifetime is what turns an ordinary short-lived return into E0623; without that indirect link, expect the "lifetime may not live long enough" report instead. - Follow the compiler's help first. When the report suggests an outlives bound such as
'in_: 'out, that bound is usually the exact promise the body was already assuming. - Check the direction before adding a bound.
'in_: 'outmeans the input must live at least as long as the output claims; adding the reverse bound compiles nothing and misstates the data flow. - Collapse to one lifetime when the two were never distinct in practice. Speculative lifetime parameters cost every caller a relationship proof; a single lifetime removes the question.
- Do not reach for
'staticto silence the mismatch. It replaces a missing relationship with the strongest possible claim, and callers holding ordinary borrowed data will fail against it.
Related guides on this site
- E0621 Explicit Lifetime Required Fix — the neighbouring error for a function whose signature omits a lifetime relationship entirely and must name one explicitly in the parameter list, where this guide covers two named lifetimes whose relationship to each other is missing.
- E0106 Missing Lifetime Specifier Fix — the error for a signature that leaves a lifetime out altogether, so the compiler cannot tell which input a returned reference borrows from.
- E0597 Does Not Live Long Enough Fix — the error for a value that is dropped while a reference to it is still in use, a question of how long data actually lives rather than how the signature describes it.
Sources
- Rust Compiler Error Index, E0623 — for a lifetime not matching what was expected, for the example of setting a value with an incompatible lifetime to another one, and for both repairs: making the input lifetime live at least as long as the output lifetime, or using only one lifetime.
- The Rust Book, Validating References with Lifetimes — for lifetime annotations describing the relationships of the lifetimes of multiple references to each other without affecting how long any reference lives, and for the syntax of lifetime parameters and outlives bounds in a function signature.
- The Rust Reference, Trait and lifetime bounds — for lifetime bounds of the form
'a: 'b, declaring that lifetime'alives at least as long as lifetime'b, and for bounds appearing in the generic parameter list of a function.
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.