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 ownership slides #7

Merged
merged 2 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
208 changes: 191 additions & 17 deletions slides/04-understanding_ownership.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -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<i32>) {
()
}
```

## `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")
}
```
Binary file added slides/img/box.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added slides/img/clone_memory.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added slides/img/dt.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added slides/img/dt_borrow.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added slides/img/dt_pretend.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added slides/img/dt_share.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added slides/img/references.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added slides/img/slice_memory.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added slides/img/stack.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added slides/img/stack_heap_move.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading