Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why mutable reference on `const` is not an error?

Tags:

rust

Since the Rust book v1.30 says explicitly:

... constants in Rust have no fixed address in memory. This is because they’re effectively inlined to each place that they’re used. References to the same constant are not necessarily guaranteed to refer to the same memory address for this reason.

why the compiler allows getting a mutable reference on a const variable. It only says a warning/note and not an error.

warning: taking a mutable reference to a `const` item
 --> src/main.rs:5:22
  |
6 |     println!("{:p}", &mut VALUE);
  |                      ^^^^^^^^^^
  |
  = note: `#[warn(const_item_mutation)]` on by default
  = note: each usage of a `const` item creates a new temporary
  = note: the mutable reference will refer to this temporary, not the original `const` item

To test this, a trivial code sample:

fn main() {
    const VALUE: u64 = 0;
    println!("{:p}", &VALUE);      // 0x10622ed78 // same
    println!("{:p}", &VALUE);      // 0x10622ed78
    println!("{:p}", &mut VALUE);  // 0x7ffee9a08890 // different
    println!("{:p}", &mut VALUE);  // 0x7ffee9a088e8
}

Rust playground

As expected, the memory location of the const may change (specially when accessed using a mutable reference).

like image 911
Pascal H. Avatar asked May 31 '26 20:05

Pascal H.


1 Answers

There are some cases where it will behave predictably. In particular, if you re-use the same reference:

const VALUE: u64 = 0;

fn main() {
    let v = &mut VALUE;
    add_1(v);
    add_1(v);
    assert_eq!(*v, 2);
}

fn add_1(v: &mut u64) {
    *v += 1;
}

I can't immediately think of a case where doing this is beneficial compared with adding a local binding first. But it can't cause memory unsafety, so it isn't a huge worry.

Given that this was not an error in Rust version 1.0, the Rust developers cannot later make it an error, because that would break backwards compatibility.

like image 174
Peter Hall Avatar answered Jun 03 '26 15:06

Peter Hall



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!