Skip to content

Discourage use of static mut in singleton #389

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
EvansJahja opened this issue Apr 17, 2025 · 4 comments · May be fixed by #390
Open

Discourage use of static mut in singleton #389

EvansJahja opened this issue Apr 17, 2025 · 4 comments · May be fixed by #390

Comments

@EvansJahja
Copy link

The code in this part about Singleton no longer works as of Rust 2024

https://docs.rust-embedded.org/book/peripherals/singletons.html

static mut PERIPHERALS: Peripherals = Peripherals {
    serial: Some(SerialPort),
};

giving error instead of warning "mutable reference to mutable static"

@BartMassey
Copy link
Member

"error: creating a mutable reference to mutable static is discouraged"? Yeah, we should stick #allow(static_mut_refs)] somewhere. A pull request would be welcome. Thanks much for reporting this.

@EvansJahja
Copy link
Author

I'm a beginner myself and not sure about the best practice.

Would using static (without mut) followed by &raw mut better? I think this is the preferrable syntax and also gives opportunity to explain.

Other would be UnsafeCell or UnsafeSyncCell.

I could try opening a PR and maybe others can point out if my understanding is sound or not

@BartMassey
Copy link
Member

Honestly, there's nothing wrong with the code as it stands as far as I know. That said, those other possibilities are there too.

Feel free to post some code either in a PR or just attach it here and we'll be happy to look at it.

@EvansJahja
Copy link
Author

Pushed #390 just now. Any feedback would be appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants