---
title: "Yours, Mine, Ours"
theme: default
layout: center
fonts:
sans: "CaskaydiaCove Nerd Font Mono"
---
# Yours, Mine, Ours
Understanding ownership and borrowing in Rust
---
layout: center
---
# Think about a book
Rust treats data a lot like a physical book.
---
layout: center
---
# Three rules to remember
```rust{|1|2}
let owner_one = String::from("The Rust Book");
let owner_two = owner_one;
```
owner_one is the owner.
owner_one transferred ownership to owner_two.
owner_one is invalid now.
---
layout: center
---
# Sometimes we don't want to buy the book
```rust{|1|2|3-4}
let owner = String::from("The Rust Book");
let borrower = &owner;
println!("I borrowed: {} from the owner!", borrower);
println!("The owner still has: {}", owner);
```
owner is the owner.
borrower borrows from owner.
Both are valid.
---
layout: center
---
# Sometimes I want to add notes to the book
```rust{|1|2|3-4}
let mut owner = String::from("The Rust Book");
let borrower = &mut owner;
borrower.push_str(" - with my notes!");
println!("The owner has: {}", owner);
```
owner is the owner.
borrower mutably borrows from owner.
The owner is mutated through the borrower.
---
layout: center
---
# We have to be thoughtful when borrowing
You don't want to change the book while someone else is reading it.