The Box Smart Pointer in Rust

preview_player
Показать описание
The ultimate Rust lang tutorial. Follow along as we go through the Rust lang book chapter by chapter.

Chapters:
0:00​ Intro
0:29 Pointers, References, and Smart Pointers
2:38 Using a Box to Store Data
4:43 Enabling Recursive Types with Boxes
11:53 Outro

#letsgetrusty​​ #rust​lang​ #tutorial
Рекомендации по теме
Комментарии
Автор

The following code works:

#[derive(Debug)]
enum List<'a> {
Cons(i32, &'a List<'a>),
Nil,
}

use List::{Cons, Nil};

fn main() {
let list = Cons(1, &Cons(2, &Cons(3, &Nil)));
println!("{:?}", list);
}

$ cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.01s
Running `target/debug/rust-demo`
Cons(1, Cons(2, Cons(3, Nil)))

So I think using cons list as an example is inappropriate to show what Box can do, but regular reference cannot do.

long-live-linux
Автор

Congrats! You are half way through the tutorial series! Keep up the momentum!
Thanks for the great videos bogdan!

brianteague
Автор

You do a great job in explaining these things. Thank you for the videos.

thingsiplay
Автор

Golang beginners - Pointers are so hard to understand
Rusty Bogdan - Regular pointers aren't cool enough

GolangDojo
Автор

Didn't even want to learn Rust... But, the presentation and bite sized nature is easily digestable!

dazealex
Автор

A very clear explanation and a good example. Thanks for the video!

高主任-gj
Автор

i have gotten a lot out of your videos thanks dude!!

jonxslays
Автор

I know that you are calling this a "cons list" but this is just an old school linked list. Cons might be from lisp, but linked lists are language agnostic

babajagaisyou
Автор

Seems like you could also get away with using Option<List> right? Is Option<T> a smart pointer type as well since it can be None?

EDIT: Nope. Option<T> needs to know the size of T

EbonySeraphim
Автор

Why we need the use statement at 6:43? Assuming an enum is like a type, what does it mean "to bring a type into scope", and why isn't it already in scope right after being defined?

sakuranooka
Автор

Interesting! But now I'm wondering: could a & reference be used instead of Box? What are the upsides to using Box in this example?

Michael-enub
Автор

Learning and enjoying your videos 👍 I think none would be the proper syntax here. No nils in Rust

Comdevsunion
Автор

Awesome! Keep it up! Hope you will soon switch to 2 videos per week :)

reinismu
Автор

more usefull use of Box is using traits, when you just store object as trait

tandex
Автор

Doesn't an enum need additional memory to specify which of the variants is actually stored?

alagaika
Автор

I really liked your explanations. I just have a question about the recursive list structure. You mentioned at the end of the video that line 9 is not very readable. It got me thinking if there would be any other nicer way of making it more readable but using the same recursive data structure approach. Do you have any idea in mind? keep rolling the videos!! :)

xad
Автор

7:20 getting flashbacks from functional programming pls put a trigger waring there xd

Nico-rlbo
Автор

O wow I just realized that a rust enum is basically a union

redcrafterlppa
Автор

I found this part of the book confusing :(, in the book it doesn't show that rust knows list is of type List, but in your VScode we can see the type, but it's not clear how it was able to infer the type. Why it doesn't treat list as "Cons" for example?

DaviAreias
Автор

Isnt that just a link list and if not what is the difference 🤨

XFajk