Email me

Rust errors / Ownership and borrowing

Rust compiler error · checked with rustc 1.99.0

Rust error E0492: interior mutable shared borrows of temporaries that have their lifetime extended until the end of the program are not allowed

What it meansA borrow of a constant containing interior mutability was attempted.

What rustc prints

error[E0492]: interior mutable shared borrows of temporaries that have their lifetime extended until the end of the program are not allowed
 --> main.rs:4:33
  |
4 | const B: &'static AtomicUsize = &A;
  |                                 ^^ this borrow of an interior mutable value 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`

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.

E0492Ownership and borrowing
Activein rustc 1.99.0
8related errors linked below

The compiler team's explanation

A borrow of a constant containing interior mutability was attempted.

Erroneous code example:

use std::sync::atomic::AtomicUsize;

const A: AtomicUsize = AtomicUsize::new(0);
const B: &'static AtomicUsize = &A;
// error: cannot borrow a constant which may contain interior mutability,
//        create a static instead

A const represents a constant value that should never change. If one takes a & reference to the constant, then one is taking a pointer to some memory location containing the value. Normally this is perfectly fine: most values can't be changed via a shared & pointer, but interior mutability would allow it. That is, a constant value could be mutated. On the other hand, a static is explicitly a single memory location, which can be mutated at will.

So, in order to solve this error, use statics which are Sync:

use std::sync::atomic::AtomicUsize;

static A: AtomicUsize = AtomicUsize::new(0);
static B: &'static AtomicUsize = &A; // ok!

You can also have this error while using a cell type:

use std::cell::Cell;

const A: Cell<usize> = Cell::new(1);
const B: &Cell<usize> = &A;
// error: cannot borrow a constant which may contain interior mutability,
//        create a static instead

// or:
struct C { a: Cell<usize> }

const D: C = C { a: Cell::new(1) };
const E: &Cell<usize> = &D.a; // error

// or:
const F: &C = &D; // error

This is because cell types do operations that are not thread-safe. Due to this, they don't implement Sync and thus can't be placed in statics.

However, if you still wish to use these types, you can achieve this by an unsafe wrapper:

use std::cell::Cell;

struct NotThreadSafe<T> {
    value: Cell<T>,
}

unsafe impl<T> Sync for NotThreadSafe<T> {}

static A: NotThreadSafe<usize> = NotThreadSafe { value : Cell::new(1) };
static B: &'static NotThreadSafe<usize> = &A; // ok!

Remember this solution is unsafe! You will have to ensure that accesses to the cell are synchronized.

From rustc --explain E0492 (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