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.
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.
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?
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.
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).
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?
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.
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?
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?
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.
📝 Get your *FREE Rust cheat sheet* : www.letsgetrusty.com/cheatsheet
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.
I'm right there with ya' Duke.
I feel the same @ChumX100
Difficult concepts become easy because of you! Thank you!
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.
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.
Thanks Bogdan, another great video!
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?
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.
Now I get it!!!! Thank you so much!
Again, nice work. Thank you.
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).
Thanks! Very helpful!
this is the hardest episode so far.
This is blowing my mind.
very good explanation, thank you
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?
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.
@@abhishek.rathore - thanks, good explanation
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?
Awesome video !!
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?
THE RUST GANG!!!
How reference (&) type is calling defef method in mybox @ 8:06, normally deref gets called by *, am I correct, am I missing anything
I do not quite understand what you meant.
Which `mybox` variable?
when will the video for asynchronous programming in Rust be ready?
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.
Good stuff! Thanks!
Hi, please do a series on async rust.
Any way you can post twice a week instead of once?
👍
Where my Rust Cheatsheet?
Coming soon! Still working on it.
this is soo hard lmao
Wish they just made C with borrow checker.
Instead of all these magic
"automagical" coercion
First...
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.
if assert_eq!(5, y) doesn't work then why println!("{y}") works?
📝 Get notified when the Rust Cheatsheet comes out ;)
letsgetrusty.com/
Rust > Go