Why does compilation not fail when a member of a moved value is assigned to?

qthree

What really happens

fn main() {
    let mut point: Point = Point { x: 0.3, y: 0.4 };
    println!("point coordinates: ({}, {})", point.x, point.y);

    drop(point);

    {
        let mut point: Point;
        point.x = 0.5;
    }

    println!(" x is {}", point.x);
}

It turns out that it's already known as issue #21232.

starblue

The problem is that the compiler allows partial reinitialization of a struct, but the whole struct is unusable after that. This happens even if the struct contains only a single field, and even if you only try to read the field you just reinitialized.

struct Test {
    f: u32,
}

fn main() {
    let mut t = Test { f: 0 };
    let t1 = t;
    t.f = 1;
    println!("{}", t.f);
}

This is discussed in issue 21232

标签