comprehensive-rust icon indicating copy to clipboard operation
comprehensive-rust copied to clipboard

Suggestion: Globals and state

Open QuineDot opened this issue 2 years ago • 1 comments

You say:

Global state is managed with static and constant variables.

But consts can't manage a state. Statics can... but preferably only with interior mutability of some sort. Your examples have no actual managing of state.

You do say "We will look at mutating static data in the chapter on Unsafe Rust", and indeed have a page on static mut.

That slide should just be deleted. It's so nigh-on impossible to get right even for experts that there's a push for its deprecation. Definitely no introduction to Rust should convey that it might be reasonable thing to reach for.


What should take its place? A more accurate portrayal of how global state is actually done in safe idiomatic Rust.

First, it usually isn't! Pass your state around, use an Arc<Mutex<_>> or the like.

But when it is, it's done with interior mutability. static atomics, thread local RefCell, once_cell (eventually to be part of std, lazy_static.


If you must leave in static mut, at least cover Once. But really that's just a tedious and error prone way of building up things like once_cell manually. In practice people just use once_cell and similar instead of rolling their own.

Reaching for unsafe to do global state yourself is just a bad idea.

QuineDot avatar Dec 27 '22 00:12 QuineDot

Hi @QuineDot, thanks a lot!

You do say "We will look at mutating static data in the chapter on Unsafe Rust", and indeed have a page on static mut.. That slide should just be deleted.

I have not actually used this particular feature myself, so this is valuable information. To be honest, the slide is only there because I wanted to at least mention all the "super powers" you get when you use the unsafe keyword. I wanted to de-mystify what it does — when teaching the class, I make a point out of telling people that unsafe doesn't "disable the borrow checker" like you sometimes see people imply.

If you must leave in static mut, at least cover Once. But really that's just a tedious and error prone way of building up things like once_cell manually. In practice people just use once_cell and similar instead of rolling their own.

We don't have to leave static mut in and I'm happy to replace the page with a write up about how to actually do it like you suggest.

In fact, your suggestion touches on something I've discussed with @qwandor and @maurer from my team: we need a set of slides which gives people an opinionated approach to Rust. Something like "Real life Rust" with a bunch of problems and suggested solutions. I already entered that a bit with the examples of how to use thiserror and anyhow for error handling. I think we need more of this.

Such a section matches feedback I got from teaching the course internally: people who are new to Rust want to be told what the best solution is by people who have already spent the time searching for a solution. The students are smart people, but we can save them a lot of time by taking them directly to a recommended solution.

mgeisler avatar Dec 27 '22 17:12 mgeisler