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
2 changes: 1 addition & 1 deletion training-slides/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Using Rust on Windows/macOS/Linux. Requires [Rust Fundamentals](#rust-fundamenta
* [Lifetimes](./lifetimes.md)
* [Cargo Workspaces](./cargo-workspaces.md)
* [Heap Allocation (Box and Rc)](./heap.md)
* [Shared Mutability (Cell, RefCell)](./shared-mutability.md)
* [Shared Mutability (Cell, RefCell, OnceCell)](./shared-mutability.md)
* [Thread Safety (Send/Sync, Arc, Mutex)](./thread-safety.md)
* [Closures and the Fn/FnOnce/FnMut traits](./closures.md)
* [Spawning Threads and Scoped Threads](./spawning-threads.md)
Expand Down
53 changes: 53 additions & 0 deletions training-slides/src/shared-mutability.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,29 @@ impl Post {
}
```

Note:

An advanced trainee may ask about the use of `Splitting Borrows` which would allow one to borrow different fields with different mutability semantics, such as
miguelraz marked this conversation as resolved.
Show resolved Hide resolved

```rust
struct Foo {
a: i32,
b: i32,
c: i32,
}

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

Which does work but doesn't solve the problem of declaring `let mut x` to begin with.

## `RefCell`

A `RefCell` is also safe, but lets you *borrow* or *mutably borrow* the contents.
Expand Down Expand Up @@ -252,3 +275,33 @@ To get *shared ownership* and *mutability* you need two things:

* `Rc<RefCell<T>>`
* (Multi-threaded programs might use `Arc<Mutex<T>>`)

## `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?


```rust
use std::time::Instant;

fn main() {
let post = Post {
content: String::from("Blah"),
..Post::default()
};
assert!(post.first_viewed_at.get().is_none());
println!("{:?}", post.hn_ranking());
assert!(post.first_viewed_at.get().is_some());
}

#[derive(Debug, Default)]
struct Post {
content: String,
first_viewed_at: std::cell::OnceCell<Instant>,
}

impl Post {
fn hn_ranking(&self) -> Instant {
jonathanpallant marked this conversation as resolved.
Show resolved Hide resolved
*self.first_viewed_at.get_or_init(|| {Instant::now()})
miguelraz marked this conversation as resolved.
Show resolved Hide resolved
}
}
```