Sling Academy
Home/Rust/Safe Global State in Rust via Once Cell and Lazy Static

Safe Global State in Rust via Once Cell and Lazy Static

Last updated: January 06, 2025

Managing global state in a thread-safe manner is one of the trickier aspects of concurrent programming. In Rust, the combination of safety guarantees and strict compiler checks can make global state a challenge but also a powerful tool when handled correctly. In this article, we'll explore how to use once_cell and lazy_static to manage safe global state in Rust applications.

Global State: The Issues

Global variables can be accessed by all threads indiscriminately, which can lead to race conditions if one thread tries to read while another thread modifies. Ensuring thread safety is the key, but doing so can introduce complexity.

Enter once_cell and lazy_static

To address these issues in Rust, libraries like once_cell and lazy_static provide services to safely initialize global variables.

Using once_cell

The once_cell crate provides constructs like OnceCell and Lazy. They ensure the data is initialized only once and provide a global view, protected from concurrent access problems.

use once_cell::sync::Lazy;
use std::collections::HashMap;

static CONFIG: Lazy<HashMap<&'static str, &'static str>> = Lazy::new(|| {
    let mut m = HashMap::new();
    m.insert("version", "1.0");
    m.insert("author", "Alice");
    m
});

fn main() {
    println!("Config Version: {}", CONFIG.get("version").unwrap());
    println!("Config Author: {}", CONFIG.get("author").unwrap());
}

In the example above, Lazy::new ensures the configuration map is initialized only once at runtime, making it efficiently accessible from any part of your program.

Using lazy_static

The lazy_static crate uses a procedural macro to achieve similar goals. While the syntax might appear different, the resultant functionality is quite similar.

#[macro_use]
extern crate lazy_static;

use std::collections::HashMap;

lazy_static! {
    static ref CONFIG: HashMap<&'static str, &'static str> = {
        let mut m = HashMap::new();
        m.insert("version", "1.0");
        m.insert("author", "Alice");
        m
    };
}

fn main() {
    println!("Config Version: {}", CONFIG.get("version").unwrap());
    println!("Config Author: {}", CONFIG.get("author").unwrap());
}

With lazy_static!, once again, initialization happens once, just when the program execution first tries to access it.

Thread Safety Considerations

Both once_cell and lazy_static automatically provide the necessary synchronization to ensure that they are safely accessed across threads.

However, remember that these solutions primarily deal with read access in a thread-safe way; if you plan to mutate the global state, other constructs such as RwLock might be necessary:

use std::sync::RwLock;

lazy_static! {
    static ref CONFIG: RwLock<HashMap<&'static str, &'static str>> = RwLock::new(HashMap::new());
}

fn update_config(key: &str, value: &str) {
    let mut map = CONFIG.write().unwrap();
    map.insert(key, value);
}

fn main() {
    update_config("project", "Example");
    let read_map = CONFIG.read().unwrap();
    println!("Project: {}", read_map.get("project").unwrap_or(&"undefined"));
}

Here, RwLock allows safe mutability by governing write-read operations, ensuring that only one write or infinite reads can happen at a time.

Conclusion

Using global state carefully and implementing tools like once_cell and lazy_static, we can construct efficient and reliable Rust applications. Once-initialization and thread safety are critical features offered by these crates, providing ease and safety in concurrent programming. By integrating these tools into your Rust arsenal, you'll prevent common pitfalls and empower your programs to handle global state safely.

Next Article: Executor Internals: How Rust Async Runtimes Schedule Tasks

Previous Article: Designing a Concurrency-Friendly API in Rust for Library Development

Series: Concurrency in Rust

Rust

You May Also Like

  • E0557 in Rust: Feature Has Been Removed or Is Unavailable in the Stable Channel
  • Network Protocol Handling Concurrency in Rust with async/await
  • Using the anyhow and thiserror Crates for Better Rust Error Tests
  • Rust - Investigating partial moves when pattern matching on vector or HashMap elements
  • Rust - Handling nested or hierarchical HashMaps for complex data relationships
  • Rust - Combining multiple HashMaps by merging keys and values
  • Composing Functionality in Rust Through Multiple Trait Bounds
  • E0437 in Rust: Unexpected `#` in macro invocation or attribute
  • Integrating I/O and Networking in Rust’s Async Concurrency
  • E0178 in Rust: Conflicting implementations of the same trait for a type
  • Utilizing a Reactor Pattern in Rust for Event-Driven Architectures
  • Parallelizing CPU-Intensive Work with Rust’s rayon Crate
  • Managing WebSocket Connections in Rust for Real-Time Apps
  • Downloading Files in Rust via HTTP for CLI Tools
  • Mocking Network Calls in Rust Tests with the surf or reqwest Crates
  • Rust - Designing advanced concurrency abstractions using generic channels or locks
  • Managing code expansion in debug builds with heavy usage of generics in Rust
  • Implementing parse-from-string logic for generic numeric types in Rust
  • Rust.- Refining trait bounds at implementation time for more specialized behavior