diff --git a/slides/04-understanding_ownership.qmd b/slides/04-understanding_ownership.qmd index 80230dd..c310038 100644 --- a/slides/04-understanding_ownership.qmd +++ b/slides/04-understanding_ownership.qmd @@ -2,30 +2,204 @@ engine: knitr title: "4. Understanding Ownership" --- +# Learning objectives -# Learning objectives +## Learning objectives -::: nonincremental -- THESE ARE NICE TO HAVE BUT NOT ABSOLUTELY NECESSARY -::: +1. Understand Rust's ownership system +2. Work with the borrow checker +3. Implement the right ownership model at the right time -::: notes -- You can add notes on each slide with blocks like this! -- Load a deck in the browser and type "s" to see these notes. -::: +# What Is Ownership? -# SLIDE SECTION +## Moves -## SLIDE +```rust +let x = String::from("hello"); +let x2 = x; +println!("{x}"); +``` -- DENOTE MAJOR SECTIONS WITH `# TITLE` (eg `# Installation`) -- ADD INDIVIDUAL SLIDES WITH `##` (eg `## rustup on Linux/macOS`) -- KEEP THEM RELATIVELY SLIDE-LIKE; THESE ARE NOTES, NOT THE BOOK ITSELF. +## Moves -## SLIDE +```rust +let x = String::from("hello"); +take_ownership(x); +println!("{x}"); -# SLIDE SECTION +fn take_ownership(_name: String) { + () +} +``` -## SLIDE +## Moves -## SLIDE +```rust +let x = String::from("hello"); +take_ownership(x.clone()); +println!("{x}"); + +fn take_ownership(_name: String) { + () +} +``` + +## Moves + +```{r} +#| eval: true +#| echo: false +knitr::include_graphics("img/clone_memory.png") +``` + +## Stack vs Heap + +```rust +let x = 1; +println!("{x}"); +println!("{x}"); +``` + +## Stack + +```{r} +#| eval: true +#| echo: false +knitr::include_graphics("img/stack.png") +``` + +## Heap + +```{r} +#| eval: true +#| echo: false +knitr::include_graphics("img/stack_heap_move.png") +``` + +## `Box::new()` + +```rust +let x = Box::new(1); +take_ownership(x); +println!("{x}"); + +fn take_ownership(_name: Box) { + () +} +``` + +## `Box::new()` + +```{r} +#| eval: true +#| echo: false +knitr::include_graphics("img/box.png") +``` + +# References and Borrowing + +## Immutable references + +```rust +let x = String::from("hello"); +borrow(&x); +println!("{x}"); + +fn borrow(_name: &String) { + () +} +``` + +## Immutable references + +```{r} +#| eval: true +#| echo: false +knitr::include_graphics("img/dt.png") +``` + +## Immutable references + +```{r} +#| eval: true +#| echo: false +knitr::include_graphics("img/dt_borrow.png") +``` + +## Immutable references + +```rust +let x = String::from("hello"); +let x2 = &x; + +borrow(&x); +println!("{x2}"); + +fn borrow(_name: &String) { + () +} +``` + +## Immutable references + +```{r} +#| eval: true +#| echo: false +knitr::include_graphics("img/dt_share.png") +``` + +## Mutable references + +```rust +let mut x = String::from("hello"); + +let x2 = &x; + +borrow(&mut x); + +println!("{x2}"); + +fn borrow(_name: &mut String) { + () +} +``` + +## Mutable references + +```{r} +#| eval: true +#| echo: false +knitr::include_graphics("img/dt_pretend.png") +``` + +# The Slice Type + +## Slices + +```{r} +#| eval: true +#| echo: false +knitr::include_graphics("img/slice_memory.png") +``` + +## String literals are slices + +```rust +let hello = "hello"; +there(hello); + +fn there(hello2: &str) { + println!("{hello2} there") +} +``` + +## `&str` as parameters + +```rust +let hello = String::from("hello"); +there(&hello); + +fn there(hello2: &str) { + println!("{hello2} there") +} +``` diff --git a/slides/img/box.png b/slides/img/box.png new file mode 100644 index 0000000..fff56cd Binary files /dev/null and b/slides/img/box.png differ diff --git a/slides/img/clone_memory.png b/slides/img/clone_memory.png new file mode 100644 index 0000000..771c338 Binary files /dev/null and b/slides/img/clone_memory.png differ diff --git a/slides/img/dt.png b/slides/img/dt.png new file mode 100644 index 0000000..0b9bfda Binary files /dev/null and b/slides/img/dt.png differ diff --git a/slides/img/dt_borrow.png b/slides/img/dt_borrow.png new file mode 100644 index 0000000..b4c70f2 Binary files /dev/null and b/slides/img/dt_borrow.png differ diff --git a/slides/img/dt_pretend.png b/slides/img/dt_pretend.png new file mode 100644 index 0000000..1d66081 Binary files /dev/null and b/slides/img/dt_pretend.png differ diff --git a/slides/img/dt_share.png b/slides/img/dt_share.png new file mode 100644 index 0000000..d024c0e Binary files /dev/null and b/slides/img/dt_share.png differ diff --git a/slides/img/references.png b/slides/img/references.png new file mode 100644 index 0000000..0a5d2c3 Binary files /dev/null and b/slides/img/references.png differ diff --git a/slides/img/slice_memory.png b/slides/img/slice_memory.png new file mode 100644 index 0000000..5a85eb8 Binary files /dev/null and b/slides/img/slice_memory.png differ diff --git a/slides/img/stack.png b/slides/img/stack.png new file mode 100644 index 0000000..1c30a55 Binary files /dev/null and b/slides/img/stack.png differ diff --git a/slides/img/stack_heap_move.png b/slides/img/stack_heap_move.png new file mode 100644 index 0000000..88857c1 Binary files /dev/null and b/slides/img/stack_heap_move.png differ