Skip to content

Conversation

@JayanAXHF
Copy link
Contributor

fixes #744

Things to check before submitting a PR

  • [] the tests are passing locally with cargo xtask test all (I'm pretty sure the link test fails for an unrelated reason)
  • commits are squashed into one and rebased to latest master
  • PR contains correct "fixes #ISSUE_ID" clause to autoclose the issue on PR merge
    • if issue does not exist consider creating it or remove the clause
  • non rendered items are in sorted order (links, reference, identifiers, Cargo.toml)
  • links to docs.rs have wildcard version https://docs.rs/tar/*/tar/struct.Entry.html
  • example has standard error handling
  • code identifiers in description are in hyperlinked backticks
[`Entry::unpack`]: https://docs.rs/tar/*/tar/struct.Entry.html#method.unpack

Things to do after submitting PR

  • check if CI is happy with your PR

Thank you for reading, you may now delete this text! Thank you! 😄

@JayanAXHF JayanAXHF changed the title Update ch 14 Update CH-14, Memory Management to add LazyCell and LazyLock Jan 9, 2026
@JayanAXHF
Copy link
Contributor Author

Output for link test:

❯❯ rust-cookbook git:(update_ch-14) 00:58 cargo xtask test link
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.15s
     Running `target/debug/xtask test link`
lychee 0.22.0
   [ERROR] Error while loading config: Cannot load configuration file `./ci/lychee.toml`: Failed to parse configuration file

Caused by:
    TOML parse error at line 26, column 1
       |
    26 | headers = []
       | ^^^^^^^
    unknown field `headers`, expected one of `files_from`, `verbose`, `no_progress`, `extensions`, `default_extension`, `cache`, `max_cache_age`, `cache_exclude_status`, `dump`, `dump_inputs`, `archive`, `suggest`, `max_redirects`, `max_retries`, `min_tls`, `max_concurrency`, `threads`, `user_agent`, `insecure`, `scheme`, `offline`, `include`, `exclude`, `exclude_file`, `exclude_path`, `exclude_all_private`, `exclude_private`, `exclude_link_local`, `exclude_loopback`, `include_mail`, `remap`, `fallback_extensions`, `index_files`, `header`, `accept`, `include_fragments`, `timeout`, `retry_wait_time`, `method`, `base_url`, `root_dir`, `basic_auth`, `github_token`, `skip_missing`, `no_ignore`, `hidden`, `include_verbatim`, `glob_ignore_case`, `output`, `mode`, `format`, `generate`, `require_https`, `cookie_jar`, `include_wikilinks`, `preprocess`

See: https://github.com/lycheeverse/lychee/blob/lychee-v0.22.0/lychee.example.toml
Failed to run link checker!

@AndyGauge
Copy link
Contributor

AndyGauge commented Jan 13, 2026

OK, so ideally we would have an example that demonstrates using LazyLock to solve a real problem, not to print. Here's an example I'd rather showcase

Deferring Expensive Computations in a Struct
This is the most common use case. Imagine a User struct where calculating "Permissions" requires a heavy database query or complex logic. You don't want to run that logic every time you load a user—only if you actually check their permissions.

use std::cell::LazyCell;

struct User {
    id: u32,
    username: String,
    // We defer the expensive permission calculation
    permissions: LazyCell<Vec<String>>,
}

impl User {
    fn new(id: u32, username: String) -> Self {
        Self {
            id,
            username,
            permissions: LazyCell::new(|| {
                println!("--- Fetching permissions from database for ID {} ---", id);
                // Simulate a heavy operation
                vec!["read".to_string(), "write".to_string()]
            }),
        }
    }
}

fn main() {
    let user = User::new(1, "ferris_rustacean".into());

    println!("User {} loaded.", user.username);

    // If we never access user.permissions, the closure is never run.
    
    if true { // Imagine a conditional check here
        println!("Permissions: {:?}", *user.permissions);
    }
}

@AndyGauge
Copy link
Contributor

Oh I see you already upated, I realized I made a mistake and provided 2 LazyLock examples

@JayanAXHF
Copy link
Contributor Author

I updated both the examples

cc: @AndyGauge

Copy link
Contributor

@AndyGauge AndyGauge left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally the descriptions are bit more explanative, but the comments do the job.

@AndyGauge AndyGauge merged commit d39c92d into rust-lang-nursery:master Jan 14, 2026
0 of 3 checks passed
@AndyGauge
Copy link
Contributor

Thanks for the report on the links, I'll try to update them and the tests to get a deployment done by the end of the month. Thank you for your contribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Also recommend LazyCell and LazyLock in Section 14. Memory Management

2 participants