Rust

Mutable Reference in Constant E0764 Fix

E0764 is the compiler refusing to let a constant or a static hold a mutable reference to a temporary. A constant is inlined and shared, so a mutable reference in its final value would make the constant itself mutable. The diagnostic underlines the borrow, explains that temporaries in constants and statics must be immutable, and points at interior-mutable statics for code that really wants global mutable state. This guide shows the error on a constant and on a static, the repairs that keep the value constant and take the reference later, and the neighboring shapes that draw different codes instead.

Written December 1, 2026 · Tested with Rust 1.99.0 · 16 min read

By the end of this guide
  • The rule underneath: no mutable reference in a final value
  • How to read the diagnostic
  • The first failure: a mutable reference in a constant
  • The same error in a static, and without 'static

Written December 1, 2026

Tested with Rust 1.99.0

What this guide is

Error E0764 is about where a mutable reference ends up, not about mutable references in general. The error codes index states it in one line - a mutable reference was used in a constant - and gives the reason: if a constant of &mut i32 type existed, the value could be modified through that reference, making the constant essentially mutable. Mutable references to temporaries are therefore allowed inside constant functions, where they stay local to the evaluation, and not in constants or statics, where they would survive in the item's final value.

The practical payoff is that the repair is a relocation, not a redesign. Keep the constant as the plain value it wraps, and take the mutable reference at the place that actually mutates - a local variable in ordinary code, or a local inside a const fn whose result is the computed value. Code that genuinely needs one shared mutable cell does not get it from a constant either: it gets a static built for the job, such as a Mutex or an atomic, both shown running below.

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: no mutable reference in a final value

A temporary written inside a constant or static initializer - the 1 in &mut 1 - would normally live only for that expression. In a constant or static, the compiler can extend such a temporary's lifetime to the end of the program, so the item can hold a reference to it. For an immutable reference that is harmless and, as the control below shows, it compiles. For a mutable reference it would create global mutable state by accident: every use of the constant would name the same mutable cell, with none of the synchronization or exclusivity a static designed for mutation provides.

That is the line the current diagnostic states directly. Its notes say that temporaries in constants and statics can have their lifetime extended until the end of the program, and that to avoid accidentally creating global mutable state, such temporaries must be immutable. Its help line then separates accident from intent: code that really wants global mutable state should replace the temporary with an interior mutable static or a static mut. The repairs below follow that split - accidental cases move the borrow, intentional cases change the item.

How to read the diagnostic

In the probed set, an E0764 failure is a single error with two notes and one help line. The header states the rule in its current wording: "mutable borrows of temporaries that have their lifetime extended until the end of the program are not allowed". The span underlines only the &mut expression and labels it "this mutable borrow refers to such a temporary". The first note explains the lifetime extension, the second states that such temporaries must be immutable, and the help line offers the interior mutable static route.

Read the underlined expression as the whole diagnosis: if the span sits on a &mut inside a const or static initializer, the code is E0764 whatever the item's declared type is - the no-'static shape below draws the identical diagnostic. If your span instead sits on a return expression inside a function, or on a function call in the initializer, a neighboring code applies; the boundary sections below cover the ones probed here.

The first failure: a mutable reference in a constant

The smallest shape is built on the index's own example — a constant declared as a static mutable reference to a temporary integer, with the result printed:

Rust
fn main() {
    const OH_NO: &'static mut usize = &mut 1;
    println!("{}", OH_NO);
}
Output
error[E0764]: mutable borrows of temporaries that have their lifetime extended until the end of the program are not allowed
 --> fail_const_mut.rs:2:39
  |
2 |     const OH_NO: &'static mut usize = &mut 1;
  |                                       ^^^^^^ this mutable borrow refers to such a temporary
  |
  = note: temporaries in constants and statics can have their lifetime extended until the end of the program
  = note: to avoid accidentally creating global mutable state, such temporaries must be immutable
  = help: if you really want global mutable state, try replacing the temporary by an interior mutable `static` or a `static mut`

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0764`.

Nothing is wrong with mutable references, with usize, or with constants in general. The one defect is the combination: a &mut whose target is a temporary, stored as the constant's final value. The index is explicit that this pattern is incorrect for exactly that reason, and that the mutable-reference work belongs inside a constant function instead - the shape shown running below.

The same error in a static, and without 'static

A static draws the identical error. The item kind does not matter, because a static's final value is just as fixed and just as shared as a constant's:

Rust
static OH_NO: &'static mut usize = &mut 1;
fn main() {
    println!("{}", OH_NO);
}
Output
error[E0764]: mutable borrows of temporaries that have their lifetime extended until the end of the program are not allowed
 --> fail_static_mut.rs:1:36
  |
1 | static OH_NO: &'static mut usize = &mut 1;
  |                                    ^^^^^^ this mutable borrow refers to such a temporary
  |
  = note: temporaries in constants and statics can have their lifetime extended until the end of the program
  = note: to avoid accidentally creating global mutable state, such temporaries must be immutable
  = help: if you really want global mutable state, try replacing the temporary by an interior mutable `static` or a `static mut`

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0764`.

Dropping the 'static annotation from the constant's type changes nothing either. The defect is the mutable borrow of the temporary, not the lifetime written in the declaration:

Rust
fn main() {
    const BAD: &mut usize = &mut 1;
    println!("{}", BAD);
}
Output
error[E0764]: mutable borrows of temporaries that have their lifetime extended until the end of the program are not allowed
 --> fail_const_mut_no_static.rs:2:29
  |
2 |     const BAD: &mut usize = &mut 1;
  |                             ^^^^^^ this mutable borrow refers to such a temporary
  |
  = note: temporaries in constants and statics can have their lifetime extended until the end of the program
  = note: to avoid accidentally creating global mutable state, such temporaries must be immutable
  = help: if you really want global mutable state, try replacing the temporary by an interior mutable `static` or a `static mut`

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0764`.

In every probed failing shape the diagnostic is identical apart from the span and the item name: a constant with &'static mut, a static with &'static mut, and a constant with a plain &mut. The failing family is one shape - a mutable borrow of a temporary as a constant or static initializer.

The control: an immutable reference in a constant compiles

The ban is on mutability, not on references. The same constant shape with an immutable reference compiles and runs, printing the value it points at:

Rust
const OK: &'static usize = &1;
fn main() {
    println!("{}", OK);
}
Output
1

The output is the pointed-at value, 1. Lifetime extension of the temporary is doing the work here, exactly as the diagnostic's first note describes - it is allowed because nothing can change the value through a shared reference to a plain value. (A shared reference to an interior-mutable temporary is a different case: the compiler bans it separately, as E0492.) When a draft of the failing code only ever reads through the reference, this is the whole repair: drop the mut.

The repair: keep the value constant, take the reference at the use site

Most code that writes a &mut constant does not need shared state at all - it needs a starting value it can mutate locally. Split the two: the constant holds the plain value, and each use copies it into a local variable and borrows that. Here the local is raised by ten through a mutable reference and printed:

Rust
const BASE: usize = 1;
fn main() {
    let mut v = BASE;
    let r = &mut v;
    *r += 10;
    println!("{}", v);
}
Output
11

The output is 11 - the starting value 1 plus the 10 added through the reference. Every caller gets its own cell, so the mutation is ordinary local mutation with no sharing to reason about. Reach for this shape first; it is the failing code with the borrow moved to where the mutation actually happens.

The repair the index shows: do the mutable work inside a const fn

When the computation itself must happen at compile time, the index's own working example is the shape: a const fn may create a local, borrow it mutably, and mutate through the borrow, as long as the reference never leaves - the function returns the computed value, and the constant holds that value. The index's example, with its result printed:

Rust
const fn foo(x: usize) -> usize {
    let mut y = 1;
    let z = &mut y;
    *z += x;
    y
}
fn main() {
    const FOO: usize = foo(10);
    println!("{}", FOO);
}
Output
11

The output is 11 - the local started at 1 and the 10 passed in was added through the mutable reference before the value was returned. The constant FOO is a plain usize; no reference appears in its type or its final value. This is the general compile-time pattern: mutable references are tools inside the evaluation, never its result.

The repair for shared state: a static built for mutation

When the intent really is one cell that code across the program mutates - the case the diagnostic's help line names - the item should be a static whose type provides the mutation discipline. A Mutex gives exclusive access through a lock, and a static Mutex can be initialized in a const context because its constructor is a const fn. Here the counter is locked, raised by ten, and read back:

Rust
use std::sync::Mutex;
static COUNTER: Mutex<usize> = Mutex::new(1);
fn main() {
    {
        let mut guard = COUNTER.lock().unwrap();
        *guard += 10;
    }
    println!("{}", *COUNTER.lock().unwrap());
}
Output
11

The output is 11, the raised value read back through a second lock. For a single integer, an atomic does the same job without a lock:

Rust
use std::sync::atomic::{AtomicUsize, Ordering};
static COUNTER: AtomicUsize = AtomicUsize::new(1);
fn main() {
    COUNTER.fetch_add(10, Ordering::SeqCst);
    println!("{}", COUNTER.load(Ordering::SeqCst));
}
Output
11

The output is again 11 - the 10 added with fetch_add to the starting 1. Both shapes state the sharing in the type, which is what the &'static mut constant tried to get without stating anything. Note the scope of these two repairs as probed here: they cover a single shared usize cell; a larger shared structure takes the Mutex form with the structure inside it.

The boundary: a const fn returning a mutable reference draws E0515

Routing the borrow through a const fn does not rescue the constant if the function tries to hand the reference back. A const fn that creates the temporary and returns a mutable reference to it fails inside the function, before any constant is evaluated, and the code is different:

Rust
const fn make() -> &'static mut usize {
    &mut 1
}
const P: &'static mut usize = make();
fn main() {
    println!("{}", P);
}
Output
error[E0515]: cannot return reference to temporary value
 --> fail_constfn_return_mut.rs:2:5
  |
2 |     &mut 1
  |     ^^^^^-
  |     |    |
  |     |    temporary value created here
  |     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`.

E0515 is the ordinary return-a-reference-to-a-local error, and it is the mechanism behind the index's remark that the borrow checker will prevent a const fn from returning new mutable references. The contrast matters for diagnosis: E0764 underlines a borrow written in the item's initializer, while E0515 underlines the return expression inside the function. Which span your error underlines tells you which side of the function boundary the temporary lives on.

The boundary: passing a mutable reference through a const fn in a constant draws E0716

A subtler route fails differently again. A const fn that merely passes a mutable reference through - returning the reference it was given - is a legal function, and calling it in ordinary code compiles and runs. Calling it in a constant, with a fresh temporary as the argument, draws neither E0764 nor E0515:

Rust
const fn pass<'a>(x: &'a mut usize) -> &'a mut usize {
    x
}
const P: &'static mut usize = pass(&mut 1);
fn main() {
    println!("{}", P);
}
Output
error[E0716]: temporary value dropped while borrowed
 --> fail_constfn_return_in_const.rs:4:41
  |
4 | const P: &'static mut usize = pass(&mut 1);
  |                               ----------^-
  |                               |         ||
  |                               |         |temporary value is freed at the end of this statement
  |                               |         creates a temporary value which is freed while still in use
  |                               using this value as a constant requires that borrow lasts for `'static`

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0716`.

The code here is E0716, "temporary value dropped while borrowed": using the call as a constant requires the borrow to last for 'static, and the temporary argument is freed at the end of the statement. The pass-through function itself is not the defect - in ordinary code, where a real variable backs the reference, it compiles and runs:

Rust
const fn leak<'a>(x: &'a mut usize) -> &'a mut usize {
    x
}
fn main() {
    let mut v = 1usize;
    let r = leak(&mut v);
    *r += 1;
    println!("{}", v);
}
Output
2

The output is 2 - the variable started at 1, the 1 added through the returned reference landed, and the variable itself is printed. So a const fn returning a mutable reference is not banned as a function; what cannot be built is a constant holding its result when the reference's target is a temporary.

The boundary: an UnsafeCell value is allowed, its raw pointer is not

Interior mutability in the value itself is not the target of this error. A constant whose value is an UnsafeCell - the type mutable references are built on - compiles and runs:

Rust
use std::cell::UnsafeCell;
const C: UnsafeCell<usize> = UnsafeCell::new(1);
fn main() {
    println!("compiled");
    let _ = &C;
}
Output
compiled

The output is the word the program prints on reaching main, compiled. What fails is extracting the cell's raw pointer as a constant: the pointer would name the temporary cell, which has no program-long storage of its own in the constant's final value, and the compiler reports a dangling pointer - an error with no error code in the probed output:

Rust
use std::cell::UnsafeCell;
const P: *mut usize = UnsafeCell::new(1).get();
fn main() {
    println!("{:p}", P);
}
Output
error: encountered dangling pointer in final value of constant
 --> boundary_unsafecell_mut_ptr_const.rs:2:1
  |
2 | const P: *mut usize = UnsafeCell::new(1).get();
  | ^^^^^^^^^^^^^^^^^^^

error: aborting due to 1 previous error

The same Sync requirement that governs every static also bounds the static form of this shape: a plain UnsafeCell is not Sync, so a static holding one directly draws E0277, the not-Sync error, rather than anything in this guide's family. Interior mutability reaches a static through a type that adds synchronization - the Mutex and atomic repairs above are exactly that.

Fixes in reach order

First, read the underlined expression. If the span sits on a &mut written directly in a const or static initializer, the code is E0764. If it sits on a return inside a const fn, the code is E0515; if it sits on a temporary passed to a call in a constant, the code is E0716; neither is repaired by editing the item's type.

Second, ask whether anything ever mutates through the reference. If the code only reads, drop the mut - an immutable reference in a constant compiles as written, as the control above shows.

Third, if each use mutates its own copy, keep the constant as the plain starting value and take the mutable reference from a local at the use site. If the mutation is part of a compile-time computation, move it inside a const fn that returns the computed value, never the reference.

Last, if the intent is genuinely one shared mutable cell, change the item kind: a static Mutex for general state, or an atomic for a single number. Do not reach for a raw pointer extracted from a constant UnsafeCell - that shape is a dangling-pointer error, not a workaround.

Decision summary

E0764, in this guide's probed set, fired exactly when a const or static initializer was a mutable borrow of a temporary: a constant typed &'static mut usize, a static typed &'static mut usize, and a constant typed &mut usize. Every diagnostic was a single error headed "mutable borrows of temporaries that have their lifetime extended until the end of the program are not allowed", underlining the &mut expression, with notes that such temporaries must be immutable and a help line pointing at an interior mutable static or a static mut. Compiling and running were: an immutable reference to a plain temporary in a constant, a constant plain value borrowed mutably from a local at the use site, a const fn that mutates a local through a mutable reference and returns the value, a const fn that passes a mutable reference through when called in ordinary code on a real variable, an UnsafeCell held as a constant value, and shared state held in a static Mutex and in a static AtomicUsize. A const fn returning a mutable reference to its own temporary drew E0515, a pass-through call on a temporary in a constant drew E0716, and a raw pointer taken from a temporary UnsafeCell in a constant was a dangling-pointer error with no code.

Checklist

  • Read the span: a &mut underlined in a const or static initializer is E0764; the expression a function returns, underlined inside the function, is E0515.
  • A constant or static cannot hold a mutable reference to a temporary - the final value would be mutable global state.
  • If the reference is only read through and the value is not interior-mutable, drop the mut - an immutable reference in a constant compiles.
  • Keep the constant as the plain value and borrow a local mutably at the use site for per-use mutation.
  • Inside a const fn, mutable references to locals are fine - return the computed value, never the reference.
  • For one shared mutable cell, use a static Mutex or an atomic, not a &'static mut constant.
  • A raw pointer from a constant UnsafeCell is a dangling-pointer error - it is not a way around E0764.

Sources

  • Rust error codes index, E0764 - a mutable reference was used in a constant; mutable references can only be used in constant functions, not statics or constants, because a constant holding one would be essentially mutable; and the working shape of a const fn that borrows a local mutably and returns the computed value, called to initialize a constant.
  • Rust error codes index, E0515 - a reference to a temporary or local value cannot be returned from a function.
  • Rust error codes index, E0716 - a temporary value is dropped while it is still borrowed.

Keep Exploring