Skip to content

It is possible to write to a field of an uninitialized struct #31947

Closed
@matklad

Description

@matklad

It is impossible to read a filed of uninitialized struct, but for some reason the write is allowed (but you can't read it back later):

struct S {x: i32, y: i32}

fn main() {
    let mut s: S;
    s.x = 32;           // why I don't get an error here,
    s.y = 12;
    println!("{}", s.x) // but get it here instead?
}

Originally reported at https://users.rust-lang.org/t/use-of-possibly-uninitialized-variable-on-an-initialized-variable/4799/4

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions