Skip to content

Commit 5f2e0d1

Browse files
committed
deploy: d39c92d
1 parent b3bf9ad commit 5f2e0d1

File tree

7 files changed

+143
-2
lines changed

7 files changed

+143
-2
lines changed

index.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,9 @@ <h1 id="hardware-support"><a class="header" href="#hardware-support">Hardware Su
375375
<h1 id="memory-management"><a class="header" href="#memory-management">Memory Management</a></h1>
376376
<div class="table-wrapper"><table><thead><tr><th>Recipe</th><th>Crates</th><th>Categories</th></tr></thead><tbody>
377377
<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>
378+
<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>
379+
<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>
380+
<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>
378381
</tbody></table>
379382
</div><!--
380383
Links, in a few categories. Follow the existing structure.

intro.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,9 @@ <h1 id="hardware-support"><a class="header" href="#hardware-support">Hardware Su
375375
<h1 id="memory-management"><a class="header" href="#memory-management">Memory Management</a></h1>
376376
<div class="table-wrapper"><table><thead><tr><th>Recipe</th><th>Crates</th><th>Categories</th></tr></thead><tbody>
377377
<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>
378+
<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>
379+
<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>
380+
<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>
378381
</tbody></table>
379382
</div><!--
380383
Links, in a few categories. Follow the existing structure.

mem.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,9 @@ <h1 class="menu-title">Rust Cookbook</h1>
162162
<h1 id="memory-management"><a class="header" href="#memory-management">Memory Management</a></h1>
163163
<div class="table-wrapper"><table><thead><tr><th>Recipe</th><th>Crates</th><th>Categories</th></tr></thead><tbody>
164164
<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>
165+
<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>
166+
<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>
167+
<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>
165168
</tbody></table>
166169
</div><!--
167170
Links, in a few categories. Follow the existing structure.

mem/global_static.html

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,69 @@ <h2 id="stdcell"><a class="header" href="#stdcell">Std:cell</a></h2>
201201
assert_eq!(value, "Hello, World!");
202202
assert!(cell.get().is_some());
203203
}</code></pre></pre>
204+
<h2 id="stdcelllazycell"><a class="header" href="#stdcelllazycell"><code>std::cell::LazyCell</code></a></h2>
205+
<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>
206+
<pre><pre class="playground"><code class="language-rust edition2021">use std::cell::LazyCell;
207+
208+
struct User {
209+
id: u32,
210+
username: String,
211+
// We defer the expensive permission calculation
212+
permissions: LazyCell&lt;Vec&lt;String&gt;&gt;,
213+
}
214+
215+
impl User {
216+
fn new(id: u32, username: String) -&gt; Self {
217+
Self {
218+
id,
219+
username,
220+
permissions: LazyCell::new(|| {
221+
println!("--- Fetching permissions from database for ID {} ---", id);
222+
// Simulate a heavy operation
223+
vec!["read".to_string(), "write".to_string()]
224+
}),
225+
}
226+
}
227+
}
228+
229+
fn main() {
230+
let user = User::new(1, "ferris_rustacean".into());
231+
232+
println!("User {} loaded.", user.username);
233+
234+
// If we never access user.permissions, the closure is never run.
235+
236+
if true { // Imagine a conditional check here
237+
println!("Permissions: {:?}", *user.permissions);
238+
}</code></pre></pre>
239+
<h2 id="stdsynclazylock"><a class="header" href="#stdsynclazylock"><code>std::sync::LazyLock</code></a></h2>
240+
<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>
241+
<pre><pre class="playground"><code class="language-rust edition2024 edition2018">use std::sync::LazyLock;
242+
use std::collections::HashMap;
243+
244+
struct Config {
245+
api_key: String,
246+
timeout: u64,
247+
}
248+
249+
// Imagine loading this from a .env file or a vault
250+
static APP_CONFIG: LazyLock&lt;Config&gt; = LazyLock::new(|| {
251+
println!("Loading configuration...");
252+
Config {
253+
api_key: std::env::var("API_KEY").unwrap_or_else(|_| "default_key".to_string()),
254+
timeout: 30,
255+
}
256+
});
257+
258+
fn main() {
259+
println!("App started.");
260+
261+
// The closure above isn't run until we access APP_CONFIG here.
262+
let timeout = APP_CONFIG.timeout;
263+
264+
println!("Timeout is: {}s", timeout);
265+
println!("API Key is hidden: {}", APP_CONFIG.api_key.len() &gt; 0);
266+
}</code></pre></pre>
204267
<!--
205268
Links, in a few categories. Follow the existing structure.
206269
Keep lines sorted.

print.html

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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><!--
378381
Links, 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><!--
38913897
Links, 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&lt;Vec&lt;String&gt;&gt;,
3953+
}
3954+
3955+
impl User {
3956+
fn new(id: u32, username: String) -&gt; 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&lt;Config&gt; = 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() &gt; 0);
4006+
}</code></pre></pre>
39384007
<!--
39394008
Links, in a few categories. Follow the existing structure.
39404009
Keep lines sorted.

searchindex.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

searchindex.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)