diff --git a/slides/A-foundations/basic-syntax.md b/slides/A-foundations/basic-syntax.md index 59466f79..1db9d82e 100644 --- a/slides/A-foundations/basic-syntax.md +++ b/slides/A-foundations/basic-syntax.md @@ -84,6 +84,7 @@ that variable ```rust fn main() { let x: i32 = 20; + // ^^^^^ Type annotation } ``` @@ -242,14 +243,14 @@ then the code for b is not executed ```rust fn main() { - let c = 'z'; + let c: char = 'z'; let z = 'ℤ'; let heart_eyed_cat = '😻'; } ``` -- A character is a 32-bit unicode scalar value -- Very much unlike C/C++ where char is 8 bits +- A `char` is a 32-bit unicode scalar value +- Very much unlike C/C++ where `char is 8 bits - -
+
- - diff --git a/slides/A-foundations/entry.md b/slides/A-foundations/entry.md index 82f2c647..16f43dea 100644 --- a/slides/A-foundations/entry.md +++ b/slides/A-foundations/entry.md @@ -3,13 +3,13 @@ theme: default class: text-center highlighter: shiki lineNumbers: true -info: "Rust - A1: Language basics" +info: "Rust - A: Foundation" drawings: persist: false fonts: mono: Fira Mono layout: cover -title: "Rust - A1: Language basics" +title: "Rust - A: Foundation" --- # Rust programming diff --git a/slides/A-foundations/images b/slides/A-foundations/images new file mode 120000 index 00000000..5e675731 --- /dev/null +++ b/slides/A-foundations/images @@ -0,0 +1 @@ +../images \ No newline at end of file diff --git a/slides/A-foundations/impl-blocks.md b/slides/A-foundations/impl-blocks.md index ff188092..e47da26b 100644 --- a/slides/A-foundations/impl-blocks.md +++ b/slides/A-foundations/impl-blocks.md @@ -1,7 +1,8 @@ + --- -# Intermission: Impl blocks -In the past few slides we saw a syntax which wasn't explained before: +# `impl` blocks +To associate functions to `structs` and `enums`, we use `impl` blocks ```rust {3} fn main() { @@ -17,7 +18,7 @@ fn main() { --- -# Intermission: Impl blocks +# `impl` blocks ```rust {all|6,13|7-12|7|17} enum IpAddress { @@ -50,7 +51,7 @@ fn main() { --- -# Intermission: Impl blocks, self and Self +# `self` and `Self` - The `self` parameter defines how the method can be used. - The `Self` type is a shorthand for the type on which the current @@ -60,19 +61,19 @@ fn main() { struct Foo(i32); impl Foo { - fn consume(self) -> Self { + fn consume(self) -> Self { // Takes `Foo` by value, returns `Foo` Self(self.0 + 1) } - fn borrow(&self) -> &i32 { + fn borrow(&self) -> &i32 { // Takes immutable reference of `Foo` &self.0 } - fn borrow_mut(&mut self) -> &mut i32 { + fn borrow_mut(&mut self) -> &mut i32 { // Takes mutable reference of `Foo` &mut self.0 } - fn new() -> Self { + fn new() -> Self { // Associated function, returns `Foo` Self(0) } } @@ -80,15 +81,15 @@ impl Foo { --- -# Intermission: Impl blocks, the self parameter +# `impl` blocks, the `self` parameter The self parameter is called the *receiver*. -* The self parameter is always the first and it always has the type on which it +* The `self` parameter is always the first and it always has the type on which it was defined -* We never specify the type of the self parameter -* We can optionally prepend `&` or `&mut ` to self to indicate that we take +* We never specify the type of the `self` parameter +* We can optionally prepend `&` or `&mut ` to `self` to indicate that we take a value by reference -* Absence of a self parameter means that the function is an associated function +* Absence of a `self` parameter means that the function is an associated function instead ```rust diff --git a/slides/A-foundations/interior-mutability.md b/slides/A-foundations/interior-mutability.md index e69de29b..1826d7e1 100644 --- a/slides/A-foundations/interior-mutability.md +++ b/slides/A-foundations/interior-mutability.md @@ -0,0 +1,8 @@ + +--- + +# To do: + +Add content on interior mutability +- `Cell` +- `RefCell` \ No newline at end of file diff --git a/slides/A-foundations/move-semantics.md b/slides/A-foundations/move-semantics.md index 6927f17a..2f495217 100644 --- a/slides/A-foundations/move-semantics.md +++ b/slides/A-foundations/move-semantics.md @@ -19,12 +19,6 @@ we need them somewhere else - ---- -layout: section ---- -# Rust's ownership model - --- layout: default --- @@ -86,7 +80,7 @@ pointer (to the current stack frame) is decreased. There are two mechanisms at play here, generally known as the stack and the heap
-
+
Frame 1
Frame 2
diff --git a/slides/A-foundations/optionals-errors.md b/slides/A-foundations/optionals-errors.md index 56b1b3dd..7484b677 100644 --- a/slides/A-foundations/optionals-errors.md +++ b/slides/A-foundations/optionals-errors.md @@ -1,3 +1,42 @@ + +--- + +# Generics +Structs become even more powerful if we introduce a little of generics + +```rust +struct PointFloat(f64, f64); +struct PointInt(i64, i64); +``` + +We are repeating ourselves here, what if we could write a data structure for +both of these cases? + + + +```rust +struct Point(T, T); + +fn main() { + let float_point: Point = Point(10.0, 10.0); + let int_point: Point = Point(10, 10); +} +``` + +Generics are much more powerful, but this is all we need for now + + + + + + --- # Option diff --git a/slides/A-foundations/ownership-borrowing.md b/slides/A-foundations/ownership-borrowing.md index 0d40a278..eda99564 100644 --- a/slides/A-foundations/ownership-borrowing.md +++ b/slides/A-foundations/ownership-borrowing.md @@ -154,13 +154,13 @@ hello, world # Rules for borrowing and references -- You may only ever have one mutable reference at the same time -- You may have any number of immutable references at the same time as long as - there is no mutable reference +- You may only ever have **one mutable reference** at the same time +- You may have **any number of immutable references** at the same time **as long as + there is no mutable reference** - References cannot *live* longer than their owners -- A reference will always at all times point to a valid value +- A reference will always at all times *point to a valid value* -These rules are enforced by the Rust compiler. +These rules are enforced by Rust's borrow checker. diff --git a/slides/A-foundations/slices.md b/slides/A-foundations/slices.md index ee32e3af..c2d50ef1 100644 --- a/slides/A-foundations/slices.md +++ b/slides/A-foundations/slices.md @@ -310,7 +310,7 @@ fn main() { --- -# str - the string slice +# `str` - the string slice It should be possible to have a reference to part of a string. But what is it? * Not `[u8]`: not every sequence of bytes is valid UTF-8 @@ -321,7 +321,7 @@ It should be possible to have a reference to part of a string. But what is it? --- -# str, String, array, Vec +# `str`, `String`, `[T; N]`, `Vec` | Static | Dynamic | Borrowed | |----------|----------|----------| @@ -335,8 +335,8 @@ It should be possible to have a reference to part of a string. But what is it? --- -# String or str -When do we use String and when do we use str? +# `String` or `str` +When do we use `String` and when do we use `str`? ```rust fn string_len(data: &String) -> usize { @@ -346,7 +346,7 @@ fn string_len(data: &String) -> usize { --- -# String or str +# `String` or `str` When do we use String and when do we use str? ```rust @@ -356,6 +356,5 @@ fn string_len(data: &str) -> usize { ``` * Prefer `&str` over `String` whenever possible -* If you need to mutate a string you might try `&mut str`, but you cannot - change a slice's length +* If you need to mutate a string you might try `&mut str`, but you cannot change a slice's length * Use `String` or `&mut String` if you need to fully mutate the string diff --git a/slides/A-foundations/smart-pointers.md b/slides/A-foundations/smart-pointers.md index 2767530d..25a3495f 100644 --- a/slides/A-foundations/smart-pointers.md +++ b/slides/A-foundations/smart-pointers.md @@ -1,18 +1,37 @@ + --- -# Boxing -There are several reasons to box a variable on the heap +# Put it in a `Box` +That pointer from the stack to the heap, how do we create such a thing? -* When something is too large to move around -* We need something that is sized dynamically -* For writing recursive data structures +* 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. ```rust -struct Node { - data: Vec, - parent: Node, +fn main() { + // put an integer on the heap + let boxed_int = Box::new(10); } ``` +
+ + + + + + +
--- @@ -39,3 +58,14 @@ struct Node { sized dynamically, but even so, a vector can be large, whereas an array will generally always have a limited size --> + +--- + +# To Do + +- Add content on the following topics: + - `Box`, `Vec` and `String` being smart pointers + - `Rc` + - `Drop` + - `Deref` & `DerefMut` + - `Deref` coercion \ No newline at end of file diff --git a/slides/A-foundations/styles b/slides/A-foundations/styles new file mode 120000 index 00000000..8f0c54c2 --- /dev/null +++ b/slides/A-foundations/styles @@ -0,0 +1 @@ +../styles \ No newline at end of file diff --git a/slides/A-foundations/traits-generics.md b/slides/A-foundations/traits-generics.md index 8bb1709e..c603d372 100644 --- a/slides/A-foundations/traits-generics.md +++ b/slides/A-foundations/traits-generics.md @@ -1,7 +1,7 @@ --- layout: section --- -# Introduction to generics +# Traits and generics --- layout: default @@ -18,6 +18,8 @@ fn add_f32(l: f32, r: f32) -> f32 { /* -snip- */ } /* ... */ ``` +No-one likes repeating themselves +
We need generic code! @@ -334,3 +336,31 @@ Trait can be implemented for a type **iff**: - Or your crate defines the type Or both, of course + +--- +layout: default +--- + +# Compiling generic functions + +```rust +impl MyAdd for i32 {/* - snip - */} +impl MyAdd for f32 {/* - snip - */} + +fn add_values(left: &T, right: &T) -> T +{ + left.my_add(right) +} + +fn main() { + let sum_one = add_values(&6, &8); + assert_eq!(sum_one, 14); + let sum_two = add_values(&6.5, &7.5); + println!("Sum two: {}", sum_two); // 14 +} +``` + +Code is monomorphized: + - Two versions of `add_values` end up in binary + - Optimized separately and very fast to run (static dispatch) + - Slow to compile and larger binary diff --git a/slides/A-foundations/vec.md b/slides/A-foundations/vec.md index 74d92611..132a5c31 100644 --- a/slides/A-foundations/vec.md +++ b/slides/A-foundations/vec.md @@ -40,10 +40,14 @@ How can a vector grow? Things on the stack need to be of a fixed size @@ -56,32 +60,3 @@ How can a vector grow? Things on the stack need to be of a fixed size add another element --> ---- - -# 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 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. - -```rust -fn main() { - // put an integer on the heap - let boxed_int = Box::new(10); -} -``` -
- - - - - - -
diff --git a/slides/A-foundations/why-rust.md b/slides/A-foundations/why-rust.md index 90a7ee60..858f60c4 100644 --- a/slides/A-foundations/why-rust.md +++ b/slides/A-foundations/why-rust.md @@ -1,25 +1,6 @@ ---- -layout: section ---- - -# Why learn Rust? - -## by Florian Gilcher - -Founder of Ferrous Systems - -Rust training and evangelisation - -Company support - -Tooling - -Ferrocene: Rust in automotive --- -layout: default ---- -# Why learn Rust? - - +# Why Rust? + +To do. Issue: [tweedegolf/101-rs#58](https://github.com/tweedegolf/101-rs/issues/58)