Skip to content

Commit

Permalink
Merge pull request #49 from rubdos/misc
Browse files Browse the repository at this point in the history
Misc changes. Credits to @rubdos
  • Loading branch information
hdoordt authored Jun 19, 2023
2 parents 9da32aa + 83514b2 commit 594a530
Show file tree
Hide file tree
Showing 19 changed files with 2,116 additions and 952 deletions.
2 changes: 1 addition & 1 deletion exercises/book/A3-traits-generics/mod.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[Slides](/slides/A3/) (or [pdf](/slides/A3-traits-generics.pdf))

## A3 Local Storage Vec
In this exercise, we'll create a type called `LocalStorageVec`, which is generic list of items that resides either on the stack or the heap, depending on its size. If its size is small enough for items to be put on the stack, the `LocalStorageVec` buffer is backed by an array. `LocalStorageVec` is not only generic over the type (`T`) of items in the list, but also by the size (`N`) of this stack-located array using a relatively new feature called 'const generics'. Once the `LocalStorageVec` contains more items than fit in the array, a heap based [`Vec`](https://doc.rust-lang.org/std/vec/struct.Vec.html) is allocated as space for the items to reside in.
In this exercise, we'll create a type called `LocalStorageVec`, which is generic list of items that resides either on the stack or the heap, depending on its size. If its size is small enough for items to be put on the stack, the `LocalStorageVec` buffer is backed by an array. `LocalStorageVec` is not only generic over the type (`T`) of items in the list, but also by the size (`N`) of this stack-located array using a relatively new feature called ["const generics"](https://doc.rust-lang.org/reference/items/generics.html#const-generics). Once the `LocalStorageVec` contains more items than fit in the array, a heap based [`Vec`](https://doc.rust-lang.org/std/vec/struct.Vec.html) is allocated as space for the items to reside in.

**Within this exercise, the objectives are annotated with a number of stars (⭐), indicating the difficulty. You are likely not to be able to finish all exercises during the turorial session**

Expand Down
11 changes: 9 additions & 2 deletions slides/A1-intro-to-rust.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ layout: default
# A new project

```bash
$ cargo init hello-world
$ cargo new hello-world
```

<v-click>
Expand Down Expand Up @@ -1070,7 +1070,14 @@ them somewhere else.

---

<img src="/images/A1-i-own-this.png" class="pl-30 h-90 float-right" />
<LightOrDark>
<template #dark>
<img src="/images/A1-i-own-this-dark.png" class="pl-30 h-90 float-right" />
</template>
<template #light>
<img src="/images/A1-i-own-this-light.png" class="pl-30 h-90 float-right" />
</template>
</LightOrDark>

# Ownership

Expand Down
65 changes: 49 additions & 16 deletions slides/A2-advanced-intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ hello, world
- References cannot *live* longer than their owners
- A reference will always at all times point to a valid value

These rules can be checked by the Rust compiler.
These rules are enforced by the Rust compiler.

<!--
- Rust tries to be smart about enforcing these rules, such that you don't notice
Expand Down Expand Up @@ -436,7 +436,7 @@ fn main() {
---

# Enumerations
But enums get more powerful, because each variant can have associated data with
Enums get more powerful, because each variant can have associated data with
it

```rust
Expand All @@ -458,12 +458,25 @@ fn main() {

* Note: an enum always is as large as the largest variant

<div class="relative">
<!--<div class="relative">-->

![Memory Layout](/images/A2-enum-memory.drawio.svg)
<div style="margin-left:auto; margin-right:auto; display:block; width:50%;">

<LightOrDark>
<template #dark>
<center>
<img src="/images/A2-enum-memory-dark.svg"/>
</center>
</template>
<template #light>
<img src="/images/A2-enum-memory-light.svg"/>
</template>
</LightOrDark>

</div>

<!--</div>-->

---

# Pattern matching
Expand All @@ -485,7 +498,7 @@ fn accept_ipv4(ip: IpAddress) {
---

# Match
But pattern matching is very powerful if combined with the match statement
Pattern matching is very powerful if combined with the match statement

```rust
fn accept_home(ip: IpAddress) {
Expand Down Expand Up @@ -982,9 +995,16 @@ fn main() {
# Vec: memory layout
How can a vector grow? Things on the stack need to be of a fixed size

<div class="relative left-130px">
<div style="margin-top: 50px; margin-left:auto; margin-right:auto; display:block; width:50%;">

![Memory Layout](/images/A2-vector-rust.drawio.svg)
<LightOrDark>
<template #dark>
<img src="/images/A2-vector-rust-dark.svg"/>
</template>
<template #light>
<img src="/images/A2-vector-rust-light.svg"/>
</template>
</LightOrDark>

</div>

Expand All @@ -1000,9 +1020,9 @@ How can a vector grow? Things on the stack need to be of a fixed size
# Put it in a box
That pointer from the stack to the heap, how do we create such a thing?

* Boxing something is the way to create data that is stored on the heap
* A box uniquely owns that data, there is no one else that also owns the same
data
* Boxing something is the way to store a value on the heap
* A `Box` uniquely owns that value, there is no one else that also owns that same
value
* Even if the type inside the box is `Copy`, the box itself is not, move
semantics apply to a box.

Expand All @@ -1012,10 +1032,16 @@ fn main() {
let boxed_int = Box::new(10);
}
```
<div style="margin-top: 50px; margin-left:auto; margin-right:auto; display:block; width:50%;">

<div class="relative left-170px">

![Memory Layout](/images/A2-box-in-memory.drawio.svg)
<LightOrDark>
<template #dark>
<img src="/images/A2-box-in-memory-dark.svg"/>
</template>
<template #light>
<img src="/images/A2-box-in-memory-light.svg"/>
</template>
</LightOrDark>

</div>

Expand Down Expand Up @@ -1095,7 +1121,7 @@ fn sum(data: &Vec<i64>) -> i64 {
---

# Slices
But what if we want something to work on arrays of any size? Or what if we want
What if we want something to work on arrays of any size? Or what if we want
to support summing up only parts of a vector?

* A slice is a dynamically sized view into a contiguous sequence
Expand Down Expand Up @@ -1187,9 +1213,16 @@ fn main() {
(but also `Box<[T]>` etc)
* The length of the slice is always stored together with the reference

<div class="relative left-170px bottom-15px">
<div style="margin-top: 50px; margin-left:auto; margin-right:auto; display:block; width:50%;">

![Memory Layout](/images/A2-slice-ptr.drawio.svg)
<LightOrDark>
<template #dark>
<img src="/images/A2-slice-ptr-dark.svg"/>
</template>
<template #light>
<img src="/images/A2-slice-ptr-light.svg"/>
</template>
</LightOrDark>

</div>

Expand Down
4 changes: 2 additions & 2 deletions slides/A3-traits-generics.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,8 @@ layout: default
# Using a `trait`

```rust{all|1-2|5-6|7-9|10-12}
// Import the type and the trait
use my_mod::{MyAdd}
// Import the trait
use my_mod::MyAdd
fn main() {
let left: u32 = 6;
Expand Down
Binary file added slides/images/A1-i-own-this-dark.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/images/A1-i-own-this-light.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 removed slides/images/A1-i-own-this.png
Binary file not shown.
172 changes: 172 additions & 0 deletions slides/images/A2-box-in-memory-dark.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 594a530

Please sign in to comment.