liveBook · Manning

State

As explained in Code Like a Pro in Rust

Global state is data accessible across an application's execution context rather than owned by a single local scope.

Cargo runs unit tests in parallel, so shared fixtures and global state must support concurrent access. Mutable statics are unsafe because aliasing violations or data races can cause undefined behavior. section 6.5

A synchronized test counter

lazy_static! { static ref COUNT: AtomicI32 = AtomicI32::new(0); } A test can then call COUNT.fetch_add(1, Ordering::SeqCst) without directly mutating a mutable static.

How Code Like a Pro in Rust covers it

How deep it goes
Explains unsafe mutable statics, atomics, Arc initialization limits, lazy_static, and mutex-based serialization.
Examples
Uses a mutable static counter, AtomicI32, Arc, lazy_static, and Mutex-protected tests as implementation examples.
What it assumes
Assumes Rust testing and concurrency familiarity while demonstrating the required synchronization patterns from unsafe failure onward.
What you can do after
Operate shared test state safely with atomics, lazy_static initialization, or mutex guards.
How it connects
Connects global state with parallel testing, threads, atomics, mutexes, lazy initialization, Arc, and libtest.

Also explained in

Compare how 39 books explain state →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Code Like a Pro in Rust. The section links point to the parts of the book it draws on.