Part of the Rust Crash Course: https://www.youtube.com/playlist?list=PL6yRaaP0WPkWRsXJgdnw9lj1vchAaKwfS
Welcome to the Rust Crash Course
In this course, we will be learning Rust from scratch. This course is very fast paced and designed for programmers who already have some experience with another programming language such as Dart or JavaScript or Swift. We will talk about variables, constants, ownership, functions, structures, enumerations, collections such as tuples, vectors, hash-maps and iterators, optionals, error handling, lifetime specifiers, traits, pointers, generics, packages, crates, modules and paths, asynchronous programming and we will also create 2 sample applications using Rust to put all these concepts to use!
Pointers lean towards advanced topics in Rust which we cannot leave out of this crash course. They allow you to have greater control over how your data is allocated and stored. In this chapter we will look at Box pointers, reference counting with Rc, weak and strong references, Cell and RefCell.
Timestamps:
00:00 - Pointers
00:25 - Stack vs heap
02:11 - Box data type
02:34 - Example of Box with dereferencing
03:46 - Deref trait on Box
04:15 - We need to implement our own box
04:33 - Define your own box
04:50 - Add "new" function in "impl" for BoxedValue
05:33 - Create an instance of BoxedValue
05:43 - Trying to dereference "value" will fail
06:23 - Implement Deref for BoxedValue
07:50 - Dereference "value"
08:11 - Point to the dereferenced value
08:52 - Point to the underlying dereferenced value
09:09 - The *ptr is a shorthand for *(ptr.deref())
10:23 - Implicit Deref coercion in functions
10:41 - Let's look at an example
10:57 - Create a boxed value
11:10 - Pass it to the function using ampersand
12:21 - "Rc" is a useful pointer
12:54 - Import Rc first
13:10 - Rc disallows mutation of the wrapped value
13:42 - Rc is single threaded
14:00 - You can create weak references to Rc
14:29 - Create a vector of String objects
14:50 - Create an Rc to the vector
15:02 - Get a weak reference to the Rc
15:43 - Drop the original Rc
15:59 - Crash when upgrading and unwrapping weak reference
16:52 - Weak references won't hold onto the underlying data
17:35 - You can use "match" on result of "upgrade()"
18:06 - Cloning an Rc creates a new Rc object
18:53 - You can clone with Rc::clone as well
19:14 - Mutability of Rc
19:28 - Import Cell
19:40 - Create a Person struct with Cell of age
20:17 - Add a function to increment age
20:56 - Create a new instance of Person
21:08 - Increment the age and print it to
22:26 - Cell allows interior mutability
22:57 - RefCell for multiple immutable borrows or 1 mutable
23:54 - RefCell is only allowed in single threaded environments
24:11 - RefCell can be borrowed immutable or mutably
24:29 - An example of where RefCell panics at runtime
25:12 - Get a mutable reference to the vector
25:35 - Get an immutable reference to the same vector
26:09 - Push a new value to the vector
26:40 - Print the length
26:52 - Run your code and observe the panic in the console
27:03 - There are ways to combine pointers
27:49 - Learning pointers in Rust is a must
If you want to support my work, please consider joining my YouTube channel by pressing the Join button!
Twitter: https://twitter.com/vandadnp
LinkedIn: https://linkedin.com/in/vandadnp
Let's get started!