Skip to content

Setting of member of const struct should not be possible #68713

Closed
@gwierzchowski

Description

@gwierzchowski

In below listing the assignment line I think should not compile with error like "invalid left-hand side expression" - as it would be in case variable was not a struct.
PS: Moving const definition to the function body does not change anything.

pub struct Foo {
    bar: bool,
}
const CONF: Foo = Foo { bar: false };

fn main() {
    println!("main 0 {}", CONF.bar); // prints false - OK
    
    // This does compile ?!?
    CONF.bar = true;

    println!("main 1 {}", CONF.bar); // prints false - ?!?
}

(Playground)

Output:

main 0 false
main 1 false

Errors:

   Compiling playground v0.0.1 (/playground)
    Finished dev [unoptimized + debuginfo] target(s) in 0.39s
     Running `target/debug/playground`

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