External Publication
Visit Post

A read only no alias reference

Rust Internals [Unofficial] June 24, 2026
Source

Another example:

struct Wrapper<'a>(&'a mut u8);

impl<'a> Wrapper<'a> {
    fn increment(&mut self) {
        *self.0 += 1;
    }
}

fn main() {
    let mut a = 13;
    let b = &mut a;
    *b += 1;
    let mut c = Wrapper(b);
    c.increment();
}

b doesn't have to be marked mut, but c has to be marked mut.

With read-only unique references the Wrapper could work like the built-in &mut type and c wouldn't need to be mut:

struct Wrapper<'a>(&'a mut u8);

impl<'a> Wrapper<'a> {
    fn increment(&^ self) {
        *self.0 += 1;
    }
}

fn main() {
    let mut a = 13;
    let b = &mut a;
    *b += 1;
    let c = Wrapper(b);
    c.increment();
}

Morgane55440:

it is a core part of the design of rust that you need mut if a variable will ever be modified, but with your idea any &^self method could suddenly be modifying non mut variable.

That's not true. Only mut variables (such as a here) or things inside types with interior mutability (such as Mutex in the previous example) could be modified, just like today.

Discussion in the ATmosphere

Loading comments...