Skip to content

Proposal - Better control for initialization #18204

Description

@NN---

Idea of keywords for better controlling variables initialization.

Samples:

mustinit - Variable that must be initialized before used.

let mut mustinit a = 1; // OK
a = 2; // OK

let mut mustinit x;
f(x); // Error: 'x' is not initialized

let mut mustinit x;
if something { x = 1; } else { x = 2; }
f(x); // OK

let mut mustinit x;
if something { x = 1; }
f(x); // Error: 'x' may not be initialized

initonce - Variable that can be initialized only once and then cannot be changed. It doesn't require the variable to be initialized.

let mut initonce a = 1; // OK
a = 2; // Error 'a' has been already initialized

let mut initonce a;
a = 1; // OK
a = 2; // Error 'a' has been already initialized

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions