Smart Pointers in Rust - The Deref Trait

  Рет қаралды 35,766

Let's Get Rusty

Let's Get Rusty

Күн бұрын

Пікірлер: 42
@letsgetrusty
@letsgetrusty 3 жыл бұрын
📝 Get your *FREE Rust cheat sheet* : www.letsgetrusty.com/cheatsheet
@ChumX100
@ChumX100 2 жыл бұрын
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 Жыл бұрын
I'm right there with ya' Duke.
@voluptua
@voluptua Жыл бұрын
I feel the same @ChumX100
@linjianru
@linjianru 2 жыл бұрын
Difficult concepts become easy because of you! Thank you!
@karlheinzneugebauer
@karlheinzneugebauer Жыл бұрын
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.
@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.
@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.
@yapayzeka
@yapayzeka 2 жыл бұрын
this is the hardest episode so far.
@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).
@idiot7leon
@idiot7leon 2 жыл бұрын
Thanks! Very helpful!
@jayleejw1801
@jayleejw1801 3 ай бұрын
THE RUST GANG!!!
@bryson2662
@bryson2662 2 жыл бұрын
This is blowing my mind.
@domtorque
@domtorque 3 жыл бұрын
Now I get it!!!! Thank you so much!
@hafizjoundysyafie8184
@hafizjoundysyafie8184 3 жыл бұрын
very good explanation, thank you
@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.
@lee45283
@lee45283 3 жыл бұрын
Thanks Bogdan, another great video!
@student99bg
@student99bg Жыл бұрын
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?
@jonathanmoore5619
@jonathanmoore5619 3 жыл бұрын
Again, nice work. Thank you.
@denizsincar29
@denizsincar29 29 күн бұрын
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?
@caseyyeow1649
@caseyyeow1649 3 жыл бұрын
when will the video for asynchronous programming in Rust be ready?
@islamibrahim8121
@islamibrahim8121 2 жыл бұрын
Hi, please do a series on async rust.
@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 Жыл бұрын
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 Жыл бұрын
@@abhishek.rathore - thanks, good explanation
@samaygandhi7182
@samaygandhi7182 Жыл бұрын
Awesome video !!
@realism44
@realism44 2 жыл бұрын
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.
@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?
@guozw4980
@guozw4980 11 ай бұрын
👍
@minhnguyen-q1z7w
@minhnguyen-q1z7w 6 ай бұрын
this is soo hard lmao
@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.
@benjaminhon86
@benjaminhon86 9 ай бұрын
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:06
Let's Get Rusty
Рет қаралды 18 М.
Rust's Alien Data Types 👽 Box, Rc, Arc
11:54
Code to the Moon
Рет қаралды 148 М.
Good teacher wows kids with practical examples #shorts
00:32
I migliori trucchetti di Fabiosa
Рет қаралды 13 МЛН
Un coup venu de l’espace 😂😂😂
00:19
Nicocapone
Рет қаралды 13 МЛН
Flipping Robot vs Heavier And Heavier Objects
00:34
Mark Rober
Рет қаралды 59 МЛН
The Box Smart Pointer in Rust
12:18
Let's Get Rusty
Рет қаралды 73 М.
Use Arc Instead of Vec
15:21
Logan Smith
Рет қаралды 149 М.
Anxiety Wars: Existentialism vs Absurdism
14:10
mystiverse
Рет қаралды 431
Smart Pointers in Rust - Reference Counting
8:41
Let's Get Rusty
Рет қаралды 25 М.
Testing in Rust
15:15
Let's Get Rusty
Рет қаралды 52 М.
India Forum 2020: Are you ready for the future workforce? Millennial & Gen Z Priorities
57:15