Skip to content

Lacking explanation of mutable globals (static mut alternatives) #4104

@kornelski

Description

@kornelski

In an error message about mutable global variables (rust-lang/rust#133143) I wanted to link to the book to a chapter explaining how to use static with mutable data, but I couldn't find one.

The closest one is about Mutex, but the chapter is written in the context of channels and non-global shared mutable state, which doesn't work as a reference for how to use static X: Mutex.

Despite global variables not being the best practice, I think a chapter dedicated to use of mutable statics is needed. Without guidance, users may discover static mut, which is even more problematic. It's not obvious that an "immutable" static can be used for mutable globals. Rust now has a new OnceLock that works for initialization of globals.

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