What is this program?

Hi all,

I'm wondering if anyone can comment on this program which seems to compile on the Rust playground:

fn main() {
    let mut x = 0; 
    { 
        let mut y = &mut x; 
        let mut z = &mut y; 
        *z = z; 
    }
}

This program appears to end up with y being borrowed to itself! Is it a bug?

fn main() {
    let mut x : i32 = 0; 
    { 
        let mut y :      &mut i32 = &mut x; 
        let mut z : &mut &mut i32 = &mut y; 
        *z = z;
        // *z: &mut i32, so z gets coerced from
        // &mut &mut i32 to &mut i32
        // same as doing *z = *z;
    }
}

References to references coerce to references to reduce indirection.


This forum is meant for discissing the Rust compiler and the specification of the Rust language. Please direct general questions to users.rust-lang.org

2 Likes

Hey Yato,

Ahh, interesting ... implicit coercion saves the day!!!

Thanks :smile:

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.