Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add OnceCell to shared-mutability + splitting burrows speaker notes #107

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

miguelraz
Copy link
Contributor

@miguelraz miguelraz commented Oct 2, 2023

Learnings from Open Intro to Rust with Aman.

@miguelraz miguelraz changed the title add OnceCell to shared-mutability add OnceCell to shared-mutability + splitting burrows speaker notes Oct 2, 2023
@miguelraz miguelraz marked this pull request as ready for review October 2, 2023 19:28
@jonathanpallant
Copy link
Member

Can you look at the pipeline failures?

@miguelraz
Copy link
Contributor Author

Pipeline failures fixed!

@jonathanpallant
Copy link
Member

Thanks! Left a few comments.

Copy link

cloudflare-workers-and-pages bot commented Jul 29, 2024

Deploying ferrous-systems-rust-training with  Cloudflare Pages  Cloudflare Pages

Latest commit: bd1d774
Status:🚫  Build failed.

View logs

@jonathanpallant
Copy link
Member

Can you rebase this on main? It's missing the build.sh script.

println!("{} {} {} {}", a, b, c, c2);
```

The code works, but you *have* to use shadowing with `let a = &mut x.a;` or else the compiler will error. The borrowchecker is particularly frail here - replacing `Foo` with `x = [1,2,3]` and trying to borrow indexes will make it error out.
Copy link
Member

Choose a reason for hiding this comment

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

I don't know that "You have to use shadowing" is true here. This code works too:

    let mut x = Foo {a: 0, b: 0, c: 0};
    let c = &x.c;
    x.b += 1;
    let c2 = &x.c;
    x.a += 10;
    println!("{} {} {} {}", x.a, x.b, c, c2);

The main take-away I think we want here is that the borrow checker is special-cased for borrowing fields in structs and tuples, and the magic there does not apply to borrowing elements using the indexing operator, like with arrays or hashmaps:

This also works:

    let mut z = (1, 2);
    let r = &z.1;
    z.0 += 1;
    println!("{:?}, {}", z, r);

This does not:

    let mut z = [1, 2];
    let r = &z[1];
    z[0] += 1;
    println!("{:?}, {}", z, r);


## `OnceCell` for special cases

If you only need to modify a field *once*, a `OnceCell` can help you keep the ownership system checks at compile-time
Copy link
Member

Choose a reason for hiding this comment

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

Can we link to the std-library docs on OnceCell?

}
```

A [LazyCell](https://doc.rust-lang.org/std/cell/struct.LazyCell.html) will do this initialization lazily, and a [LazyLock](https://doc.rust-lang.org/std/sync/struct.LazyLock.html) will do it in a threadsafe way.
Copy link
Member

Choose a reason for hiding this comment

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

I'm afraid I didn't understand the difference between OnceCell and LazyCell after reading this note. I also observe that OnceLock exists.

The primary difference is that a LazyCell owns its initialisation function - it is passed when the cell is constructed, not when the cell is accessed. See https://doc.rust-lang.org/std/cell/index.html#oncecellt and https://doc.rust-lang.org/std/cell/index.html#lazycellt-f. However both are equally 'lazy' in that the function is not called at construction time - it is only called on first access (either explicitly or implicitly).

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.

2 participants