Smart Pointers in Rust - The Deref Trait

  Рет қаралды 37,889

Let's Get Rusty

Let's Get Rusty

Күн бұрын

Пікірлер: 44
@letsgetrusty
@letsgetrusty 3 жыл бұрын
📝 Get your *FREE Rust cheat sheet* : www.letsgetrusty.com/cheatsheet
@ChumX100
@ChumX100 3 жыл бұрын
Chapter 15 was the first hard "wall" I faced learning Rust. Glad to have these videos to go over and digest the content a bit better.
@Nounearthlyglow
@Nounearthlyglow 2 жыл бұрын
I'm right there with ya' Duke.
@voluptua
@voluptua Жыл бұрын
I feel the same @ChumX100
@Gigachad-mc5qz
@Gigachad-mc5qz 9 күн бұрын
For me it was pngme book
@linjianru
@linjianru 2 жыл бұрын
Difficult concepts become easy because of you! Thank you!
@barisballi70
@barisballi70 Жыл бұрын
Wow, I was wondering why we can pass &String typed values to the functions that expect &str types, this dereferencing chain explains a lot. I know that, go also uses a similar approach when it comes to dereferencing, but go compiler is a thousand times worse when making suggestions.
@yapayzeka
@yapayzeka 2 жыл бұрын
this is the hardest episode so far.
@karlheinzneugebauer
@karlheinzneugebauer 2 жыл бұрын
Holy moly! I write in C++, PL/SQL, Bash, JS, and sometimes PHP every day and I think Rust is really hard. But thankfully we got a wonderful compiler and a teacher like you.
@mihaigalos279
@mihaigalos279 2 жыл бұрын
Bogdan, I suggest using "fn new(..) -> Self" default constructor signatures - notice the capital S. This reduces typing, ensures you don't need to propagate generic types into the output and finally, ensures you automatically propagate changes to generics into the return type (should they change).
@0scarcalderon
@0scarcalderon 2 жыл бұрын
Thank you for this detailed explanation! As English is not my native language, the word of deref or dereference doesn't make too much sense to me. It sounds for me like the reference is being removed, but it is actually about not performing operations against the reference, but "fetching" the actual value being referenced to perform operations against it right?
@Tony_Goat
@Tony_Goat 2 жыл бұрын
Yes, that's the best way to think about it. It makes more sense to use it as a verb in this context rather than a noun. To reference something means to mention it in shorthand. For example, if one piece of writing references another, they simply use the title of the mentioned work rather than including the entire work. To dereference something is to undo a reference, or rather find and turn that mention of the writing back into the full original work, so you're no longer mentioning it by title. Similarly in programming, a reference is a way to mention data in shorthand (using its memory address in the background as the title), and to dereference is to turn that mention (the address) back into the data.
@idiot7leon
@idiot7leon 2 жыл бұрын
Thanks! Very helpful!
@bryson2662
@bryson2662 2 жыл бұрын
This is blowing my mind.
@domtorque
@domtorque 3 жыл бұрын
Now I get it!!!! Thank you so much!
@lee45283
@lee45283 3 жыл бұрын
Thanks Bogdan, another great video!
@centaurii44
@centaurii44 3 жыл бұрын
You seriously need to be a CS professor. When I was reading the book I could not wrap my head around Deref but after watching this video everything made sense.
@jonathanmoore5619
@jonathanmoore5619 3 жыл бұрын
Again, nice work. Thank you.
@hafizjoundysyafie8184
@hafizjoundysyafie8184 3 жыл бұрын
very good explanation, thank you
@hussainaqeel7371
@hussainaqeel7371 3 жыл бұрын
Good stuff! Thanks!
@jeffg4686
@jeffg4686 2 жыл бұрын
I was thinking about this a minute. The deref method of Deref deferences the smart pointer to yield a reference, so it seems to do a double deref in a way as I see it if dereffing the smart pointer yields the same as the integer "value" itself. fn deref(&self) -> &Self::Target; // see, the reference is returned, not the value So compiler must be doing some kind of double duty here, apply another deference on the reference returned from deref to get actual underlying value. idk, something they don't explain well at this point. Cause if deref returns a reference, how could it equal "5" literal integer value?
@abhishek.rathore
@abhishek.rathore 2 жыл бұрын
Yes. When he said *y is same as *(y.deref()) this double derefencing thing is implied. y.deref() gives us a reference to the underlying data which can be considered wrongly named as it is not dereferencing but is returning a reference. But since Rust automatically converts *y into this latter form, it actually is derefencing by getting data from the reference returned by the deref function. So by the name it is double derefencing but in actuality it is being dereferenced only once and that is after calling the deref function.
@jeffg4686
@jeffg4686 2 жыл бұрын
@@abhishek.rathore - thanks, good explanation
@1234fewgfwe
@1234fewgfwe 6 ай бұрын
THE RUST GANG!!!
@caseyyeow1649
@caseyyeow1649 3 жыл бұрын
when will the video for asynchronous programming in Rust be ready?
@DipsAndPushups
@DipsAndPushups Жыл бұрын
If you put an instance of a struct that doesn't implement deref trait into a box then you would not be able to dereference the box right? If I write let a = Box::from(MyStruct::new()); I will not be able to write *a unless MyStruct implements the deref trait, correct?
@bjugdbjk
@bjugdbjk 2 жыл бұрын
How reference (&) type is calling defef method in mybox @ 8:06, normally deref gets called by *, am I correct, am I missing anything
@FaranAiki
@FaranAiki 2 жыл бұрын
I do not quite understand what you meant. Which `mybox` variable?
@islamibrahim8121
@islamibrahim8121 2 жыл бұрын
Hi, please do a series on async rust.
@samaygandhi7182
@samaygandhi7182 2 жыл бұрын
Awesome video !!
@denizsincar29
@denizsincar29 3 ай бұрын
Does dereferencing a reference return the original type? Than how borrowing rules apply? let a = String::new(); let b = &a; let c=*b; // c is a? Than what happens if i dereference a referenced parameter inside a function? Does it still work with the reference?
@guozw4980
@guozw4980 Жыл бұрын
👍
@abhinavchavali1443
@abhinavchavali1443 3 жыл бұрын
Any way you can post twice a week instead of once?
@ИнякинАлександр
@ИнякинАлександр 3 жыл бұрын
Where my Rust Cheatsheet?
@letsgetrusty
@letsgetrusty 3 жыл бұрын
Coming soon! Still working on it.
@hermannpaschulke1583
@hermannpaschulke1583 3 жыл бұрын
Also, you can simply do the dot operator "." on types that implement Deref. Like so : x.foo(). In C and C++ you need to use the arrow -> instead.
@minhnguyen-q1z7w
@minhnguyen-q1z7w 9 ай бұрын
this is soo hard lmao
@TejasPatil0
@TejasPatil0 Ай бұрын
this is a beutiful language
@benjaminhon86
@benjaminhon86 Жыл бұрын
Wish they just made C with borrow checker. Instead of all these magic
@MrGreen-kq4ds
@MrGreen-kq4ds 3 жыл бұрын
"automagical" coercion
@dagher.a247
@dagher.a247 3 жыл бұрын
First...
@rishabhagrawalzra
@rishabhagrawalzra Жыл бұрын
if assert_eq!(5, y) doesn't work then why println!("{y}") works?
@GolangDojo
@GolangDojo 3 жыл бұрын
📝 Get notified when the Rust Cheatsheet comes out ;) letsgetrusty.com/
@abhinavchavali1443
@abhinavchavali1443 3 жыл бұрын
Rust > Go
Smart Pointers in Rust - The Drop Trait
5:07
Let's Get Rusty
Рет қаралды 19 М.
The Box Smart Pointer in Rust
12:18
Let's Get Rusty
Рет қаралды 76 М.
«Жат бауыр» телехикаясы І 30 - бөлім | Соңғы бөлім
52:59
Qazaqstan TV / Қазақстан Ұлттық Арнасы
Рет қаралды 340 М.
UFC 287 : Перейра VS Адесанья 2
6:02
Setanta Sports UFC
Рет қаралды 486 М.
8 deadly mistakes beginner Rust developers make
14:14
Let's Get Rusty
Рет қаралды 179 М.
Rust's Alien Data Types 👽 Box, Rc, Arc
11:54
Code to the Moon
Рет қаралды 156 М.
Smart Pointers in Rust - Reference Counting
8:41
Let's Get Rusty
Рет қаралды 27 М.
Smart Pointers in Rust - Interior Mutability
17:04
Let's Get Rusty
Рет қаралды 45 М.
Iterators in Rust
13:25
Let's Get Rusty
Рет қаралды 55 М.
Rust's second most complicated feature explained
7:48
Let's Get Rusty
Рет қаралды 39 М.
Using Trait Objects in Rust
13:32
Let's Get Rusty
Рет қаралды 42 М.
Breaking Rust’s memory safety with 1 line of code
4:37
Let's Get Rusty
Рет қаралды 14 М.
All Rust features explained
21:30
Let's Get Rusty
Рет қаралды 335 М.
Generic Types in Rust
15:18
Let's Get Rusty
Рет қаралды 75 М.