Mutable reference and pointer aliasing

228 Views Asked by At

Consider the following code:

unsafe {
    let mut s = "".to_string();
    let r = &mut s;
    let ptr = r as *mut String;
        
    r.push('a');
    (*ptr).push('b');
    r.push('c');
    (*ptr).push('d');
        
    println!("{}", r);
}

Playground.

I would think that this is a clear violation of Rust aliasing rules: I have a mutable reference and a mutable pointer, and there are interleaving writes using both of them. However, MIRI lets it pass. What am I missing?

0

There are 0 best solutions below