@@ -373,6 +373,9 @@ <h1 id="hardware-support"><a class="header" href="#hardware-support">Hardware Su
373373< h1 id ="memory-management "> < a class ="header " href ="#memory-management "> Memory Management</ a > </ h1 >
374374< div class ="table-wrapper "> < table > < thead > < tr > < th > Recipe</ th > < th > Crates</ th > < th > Categories</ th > </ tr > </ thead > < tbody >
375375< tr > < td > < a href ="mem/global_static.html#declare-lazily-evaluated-constant "> Declare lazily evaluated constant</ a > </ td > < td > < a href ="https://docs.rs/lazy_static/ "> < img src ="https://badge-cache.kominick.com/crates/v/lazy_static.svg?label=lazy_static " alt ="lazy_static-badge " /> </ a > </ td > < td > < a href ="https://crates.io/categories/caching "> < img src ="https://badge-cache.kominick.com/badge/caching--x.svg?style=social " alt ="cat-caching-badge " /> </ a > < a href ="https://crates.io/categories/rust-patterns "> < img src ="https://badge-cache.kominick.com/badge/rust_patterns--x.svg?style=social " alt ="cat-rust-patterns-badge " /> </ a > </ td > </ tr >
376+ < tr > < td > < a href ="mem/global_static.html#stdcell "> Std::cell</ a > </ td > < td > < a href ="https://doc.rust-lang.org/std "> < img src ="https://badge-cache.kominick.com/badge/std-1.29.1-blue.svg " alt ="std-badge " /> </ a > </ td > < td > < a href ="https://crates.io/categories/caching "> < img src ="https://badge-cache.kominick.com/badge/caching--x.svg?style=social " alt ="cat-caching-badge " /> </ a > < a href ="https://crates.io/categories/rust-patterns "> < img src ="https://badge-cache.kominick.com/badge/rust_patterns--x.svg?style=social " alt ="cat-rust-patterns-badge " /> </ a > </ td > </ tr >
377+ < tr > < td > < a href ="mem/global_static.html#stdcelllazycell "> < code > std::cell:LazyCell</ code > </ a > </ td > < td > < a href ="https://doc.rust-lang.org/std "> < img src ="https://badge-cache.kominick.com/badge/std-1.29.1-blue.svg " alt ="std-badge " /> </ a > </ td > < td > < a href ="https://crates.io/categories/caching "> < img src ="https://badge-cache.kominick.com/badge/caching--x.svg?style=social " alt ="cat-caching-badge " /> </ a > < a href ="https://crates.io/categories/rust-patterns "> < img src ="https://badge-cache.kominick.com/badge/rust_patterns--x.svg?style=social " alt ="cat-rust-patterns-badge " /> </ a > </ td > </ tr >
378+ < tr > < td > < a href ="mem/global_static.html#stdsynclazylock "> < code > std::sync::LazyLock</ code > </ a > </ td > < td > < a href ="https://doc.rust-lang.org/std "> < img src ="https://badge-cache.kominick.com/badge/std-1.29.1-blue.svg " alt ="std-badge " /> </ a > </ td > < td > < a href ="https://crates.io/categories/caching "> < img src ="https://badge-cache.kominick.com/badge/caching--x.svg?style=social " alt ="cat-caching-badge " /> </ a > < a href ="https://crates.io/categories/rust-patterns "> < img src ="https://badge-cache.kominick.com/badge/rust_patterns--x.svg?style=social " alt ="cat-rust-patterns-badge " /> </ a > </ td > </ tr >
376379</ tbody > </ table >
377380</ div > <!--
378381Links, in a few categories. Follow the existing structure.
@@ -3886,6 +3889,9 @@ <h2 id="check-number-of-logical-cpu-cores"><a class="header" href="#check-number
38863889< div style ="break-before: page; page-break-before: always; "> </ div > < h1 id ="memory-management-1 "> < a class ="header " href ="#memory-management-1 "> Memory Management</ a > </ h1 >
38873890< div class ="table-wrapper "> < table > < thead > < tr > < th > Recipe</ th > < th > Crates</ th > < th > Categories</ th > </ tr > </ thead > < tbody >
38883891< tr > < td > < a href ="mem/global_static.html#declare-lazily-evaluated-constant "> Declare lazily evaluated constant</ a > </ td > < td > < a href ="https://docs.rs/lazy_static/ "> < img src ="https://badge-cache.kominick.com/crates/v/lazy_static.svg?label=lazy_static " alt ="lazy_static-badge " /> </ a > </ td > < td > < a href ="https://crates.io/categories/caching "> < img src ="https://badge-cache.kominick.com/badge/caching--x.svg?style=social " alt ="cat-caching-badge " /> </ a > < a href ="https://crates.io/categories/rust-patterns "> < img src ="https://badge-cache.kominick.com/badge/rust_patterns--x.svg?style=social " alt ="cat-rust-patterns-badge " /> </ a > </ td > </ tr >
3892+ < tr > < td > < a href ="mem/global_static.html#stdcell "> Std::cell</ a > </ td > < td > < a href ="https://doc.rust-lang.org/std "> < img src ="https://badge-cache.kominick.com/badge/std-1.29.1-blue.svg " alt ="std-badge " /> </ a > </ td > < td > < a href ="https://crates.io/categories/caching "> < img src ="https://badge-cache.kominick.com/badge/caching--x.svg?style=social " alt ="cat-caching-badge " /> </ a > < a href ="https://crates.io/categories/rust-patterns "> < img src ="https://badge-cache.kominick.com/badge/rust_patterns--x.svg?style=social " alt ="cat-rust-patterns-badge " /> </ a > </ td > </ tr >
3893+ < tr > < td > < a href ="mem/global_static.html#stdcelllazycell "> < code > std::cell:LazyCell</ code > </ a > </ td > < td > < a href ="https://doc.rust-lang.org/std "> < img src ="https://badge-cache.kominick.com/badge/std-1.29.1-blue.svg " alt ="std-badge " /> </ a > </ td > < td > < a href ="https://crates.io/categories/caching "> < img src ="https://badge-cache.kominick.com/badge/caching--x.svg?style=social " alt ="cat-caching-badge " /> </ a > < a href ="https://crates.io/categories/rust-patterns "> < img src ="https://badge-cache.kominick.com/badge/rust_patterns--x.svg?style=social " alt ="cat-rust-patterns-badge " /> </ a > </ td > </ tr >
3894+ < tr > < td > < a href ="mem/global_static.html#stdsynclazylock "> < code > std::sync::LazyLock</ code > </ a > </ td > < td > < a href ="https://doc.rust-lang.org/std "> < img src ="https://badge-cache.kominick.com/badge/std-1.29.1-blue.svg " alt ="std-badge " /> </ a > </ td > < td > < a href ="https://crates.io/categories/caching "> < img src ="https://badge-cache.kominick.com/badge/caching--x.svg?style=social " alt ="cat-caching-badge " /> </ a > < a href ="https://crates.io/categories/rust-patterns "> < img src ="https://badge-cache.kominick.com/badge/rust_patterns--x.svg?style=social " alt ="cat-rust-patterns-badge " /> </ a > </ td > </ tr >
38893895</ tbody > </ table >
38903896</ div > <!--
38913897Links, in a few categories. Follow the existing structure.
@@ -3935,6 +3941,69 @@ <h2 id="stdcell"><a class="header" href="#stdcell">Std:cell</a></h2>
39353941 assert_eq!(value, "Hello, World!");
39363942 assert!(cell.get().is_some());
39373943}</ code > </ pre > </ pre >
3944+ < h2 id ="stdcelllazycell "> < a class ="header " href ="#stdcelllazycell "> < code > std::cell::LazyCell</ code > </ a > </ h2 >
3945+ < p > < a href ="https://doc.rust-lang.org/std/cell/struct.LazyCell.html "> < code > LazyCell</ code > </ a > and its thread-safe counterpart < a href ="https://doc.rust-lang.org/std/sync/struct.LazyLock.html "> < code > LazyLock</ code > </ a > can be used to create a value which is initialized on the first access.</ p >
3946+ < pre > < pre class ="playground "> < code class ="language-rust edition2021 "> use std::cell::LazyCell;
3947+
3948+ struct User {
3949+ id: u32,
3950+ username: String,
3951+ // We defer the expensive permission calculation
3952+ permissions: LazyCell<Vec<String>>,
3953+ }
3954+
3955+ impl User {
3956+ fn new(id: u32, username: String) -> Self {
3957+ Self {
3958+ id,
3959+ username,
3960+ permissions: LazyCell::new(|| {
3961+ println!("--- Fetching permissions from database for ID {} ---", id);
3962+ // Simulate a heavy operation
3963+ vec!["read".to_string(), "write".to_string()]
3964+ }),
3965+ }
3966+ }
3967+ }
3968+
3969+ fn main() {
3970+ let user = User::new(1, "ferris_rustacean".into());
3971+
3972+ println!("User {} loaded.", user.username);
3973+
3974+ // If we never access user.permissions, the closure is never run.
3975+
3976+ if true { // Imagine a conditional check here
3977+ println!("Permissions: {:?}", *user.permissions);
3978+ }</ code > </ pre > </ pre >
3979+ < h2 id ="stdsynclazylock "> < a class ="header " href ="#stdsynclazylock "> < code > std::sync::LazyLock</ code > </ a > </ h2 >
3980+ < p > The < a href ="https://doc.rust-lang.org/std/sync/struct.LazyLock.html "> < code > LazyLock</ code > </ a > type is a thread-safe alternative to < a href ="https://doc.rust-lang.org/std/cell/struct.LazyCell.html "> < code > LazyCell</ code > </ a > .</ p >
3981+ < pre > < pre class ="playground "> < code class ="language-rust edition2024 edition2018 "> use std::sync::LazyLock;
3982+ use std::collections::HashMap;
3983+
3984+ struct Config {
3985+ api_key: String,
3986+ timeout: u64,
3987+ }
3988+
3989+ // Imagine loading this from a .env file or a vault
3990+ static APP_CONFIG: LazyLock<Config> = LazyLock::new(|| {
3991+ println!("Loading configuration...");
3992+ Config {
3993+ api_key: std::env::var("API_KEY").unwrap_or_else(|_| "default_key".to_string()),
3994+ timeout: 30,
3995+ }
3996+ });
3997+
3998+ fn main() {
3999+ println!("App started.");
4000+
4001+ // The closure above isn't run until we access APP_CONFIG here.
4002+ let timeout = APP_CONFIG.timeout;
4003+
4004+ println!("Timeout is: {}s", timeout);
4005+ println!("API Key is hidden: {}", APP_CONFIG.api_key.len() > 0);
4006+ }</ code > </ pre > </ pre >
39384007<!--
39394008Links, in a few categories. Follow the existing structure.
39404009Keep lines sorted.
0 commit comments