External Publication
Visit Post

A read only no alias reference

Rust Internals [Unofficial] June 24, 2026
Source

It would allow dropping a superfluous mut on some variables, for instance:

fn f() {
    let vector = Mutex::new(vec![1,2,3]);
    let mut vector_guard = vector.lock().unwrap();
    vector_guard.push(4);
}

The variable vector_guard needs to be marked mut because MutexGuard::deref_mut takes a &mut self where it only really needs a &^ self. The guard doesn't actually get modified.

By way of analogy a variable of type &mut T doesn't need to be mutable to be dereferenced, it only needs to be unique. The variable vector_ref does not need to be marked mut:

fn g() {
    let mut vector = vec![1,2,3];
    let vector_ref = &mut vector;
    vector_ref.push(4);
}

Discussion in the ATmosphere

Loading comments...