As a line-of-business developer I've been a bit intimidated about learning an innovative system language like Rust - but this makes one of the gnarliest features seem learnable. You're a good teacher.
I don’t know why, but the borrow checker doesn’t confuse me that much. Use a reference when you want to see the original memory through a window, move when you want the data for yourself, and never have a window to a place that does not exist.
This is by far the best explanation about Rust's borrow mechanism I've encountered so far! The course is an excellent resource for Rust newcomers. Great work!
10:53 "When we pass in parameters into a function its the same as if we were to assign s into another variable" - now that's when the borrow checker finally clicked for me. Now I also understand why it's so controversial to some people. Your tutorial so clear and easy to understand. Thank you!
I think that was the best explanation I've come across so far...you took the time a noobie would need to learn this stuff, and didn't try to "keep under two minutes" destroying the clarity for the sake of speed. Also, you didn't jump immediately to metaphors of "oh, its like if you have a book, and you lend it to someone, but that person can write on the book" and so on...I also find those unnecessary and confusing sometimes
7:57 - why would you have to use new to allocate memory on the heap in C++? Just use std::string s("hello"), or std::vector and it works just like it did since 30 something years, memory is deallocated at the end of scope.
True, you don't have to manage the memory yourself with raii data structures. Be aware these data structures are still doing dynamic allocation, but they are doing it in a safe way. You still take a potential performance hit but memory leaks shouldn't be an issue.
You can use a std::array if you want to avoid the free store and use automatic(commonly referred to as stack) storage. Std::array requires a size known at compile time.
This is really fantastic! Your cadence, examples and explanations are really great! I've been programming for 25 years (C++, C#, Js, etc etc) and this is a really nice way to understand nuances of rust. Thank you!
So I recently just made my first foray into Rust by attempting to build a calculator in a Yew app. I am still struggling with the logic itself, but I actually found the battle with the borrow checker to be one of the more refreshing sorts of problems I ran into. It made me think so much harder about where I was declaring my variables and where I was mutating them that it kinda just felt like my brain was steadily increasing in mass and wrinkle count the whole time.
I am new to Rust, read ownership chapter twice, got the concepts of ownership, reference and moves but still wasn't confident. This video did the trick and made everything crystal clear. Thanks a ton!
I read the book and i was overwhelmed of new terms and information. this video helped me to visualize it live. yes rust book visualizations were great but for me i find this more helpful. i think after watching this, i will understand the book better. thank you. there are too little learning resources for rust :)
I have watched tilll 17:19 and can say the explanation is top notch took me 45 min to react this point as I am coding as well the information which I find useful will continue from here the next day
Very nice series As a hobbyist who's dabbled in a bunch of languages because it's fun, i'm now learning me some rust. Certainly doing my share of fighting with the borrow checker but that said, i'm super impressed with the errors and warnings that the compiler spits out. Most helpful messages that i've encountered in any language; have helped me sort out a bunch of things which in other languages i would have had to fire up the google to work out what was going on.
This is a bit old, but in case somebody else is with the same question, it is because in the context of the code he is presenting, the lifetime of r1 and r2 are implicitly tied to the last time they are used. They were dropped to make the definition of mutable reference r3 possible.
Honestly man, your videos have really helped me whilst I go through the book. Theres a lot of information to consume so appreciate you taking the time to make these accompanying videos (y), some things are easier to see than to read and vice versa :)
Right around 14:10 was the lightbulb moment for me. And being a long time C and C++ programmer I think I'm finally starting to see why this Rust thing makes sense... What a clever way to make sure that we know who is writing data and who is just reading or calling getters. It makes the C/C++ way of using references, pointers and const seem kind of silly.
i took an online course, it was short but still, it explained Strings and string slices. (literals) etc... but the way you explained them in this video were so much more clear. thank you.
I work with node and go for my serious business projects but I did get a offer from a firm that uses rust and pretty flexible timeline if I ever wanted to onboard. This got me into going through the rust book and learning the language… I really like it! I also liked go a lot too.. probably because I started in insane crazy js land now these relatively new languages seem to nice
What I really like about your videos, until now :-), is the speed, you tell things. I'm not native American or English, but most every tutorial I watched for instance on Udemy was so slow. Of cause someone can speedup the video, but sometimes, the speaker speaks faster or changes slides, and that results in confusion. Very, very well done, again until here 🙂as I don't know more of your videos besides the seen ones.
Man I'm getting all worked up converting my c++ program to rust. 2 days in and I am no where near to finish it. Now I've realized that my "c++ way of thinking" is getting in the way.
You're an amazing teacher thanks man. The way you expained how rust stops two mutable references in the same scope to prevent race conditions. If I am not wrong, this feature isnt there in golang, and this is where rust outshines. This feature will outshine even more, when concurrency comes into play. Explaining why rust promotes safe concurrency. Correct me if I got this right.
Coming from c++ this is intuitive. Unique pointers and move semantics give this type of behavior as options in modern c++ so having them as the default makes sense. Also having the const as default on refs is another good safety measure. I do think a basic understanding of pointers in c and references and smart pointers in c++ will help people understandownership and the ideas it's built upon.
Perfect explanation, even though the concept is hard to understand, the explanations and examples provided in this video are very valuable. Thank you for it.
Thanks for the video. I have a question about the code example in the "Stack & Heap" section: - Is there any reason why a() and b() are defined within main() scope rather than, more commonly, outside main()? - a() is actually never called by main() and therefore neither is b(). Do the stack frames get constructed anyway as in the picture?
No particular reason for a() and b() being defined inside main(). Stack frames will not be created unless the functions are called so my example code isn't 100% accurate. Good eye!
*Me* : Casually starts to watch video to understand closures After sometime , Can't stop watching other videos. I was so deeply involved in listening your videos, I noticed a background music, could you please tell me which song it is :D
I read the Rust book a couple of years ago now but never really made use of Rust because all the borrow checking and lower level things made me realize maybe I'm happier staying with C# or some higher level language. I've been wanting to get back into it, or maybe to learn Go. Rust seems quite popular these days so starting to go that way again. Your videos are very well presented, thank you for taking your time to share your knowledge in such a clear and calm way, it's very motivating, much appreciated
If you are already familiar with C#. I would actually recommend you learn a LITTLE bit of C++ before. The Syntax is kinda similar (They are oriented around OOP), but C# has a Garbage Collector, while C++ has only manual memory Management. So you'll learn about pointers, malloc and new, destructors, smart_ptrs, references (to lvalues and rvalues), copy constructor and move semantics. Those are things, C# does not have. So you can take like a day to learn the Syntax of C++ (How to do in C++ what you can do in C#) and then learn the memory management stuff which is the hard part. That way you can understand how hard and unreliable this stuff really is, and how rust, using the same abstractions (But with the added concept of borrowing (A type of move) that can be tracked by the borrow checker) can solve the same issues of C++, and a lot more, by default at compile time. And all the weird anoying part of the languages that you struggled with will probably now make sense as a logical next step, or a consequence of the things that you now know are happening under the hood!
You could also just keep going. Rust is s logical simplification of C++. But C++ is just a superset of C created to simplify reasoning about objects instead of just bytes in memory, and as such allowing things like RAII which are imposible in C. And C is just a logical next step above assembly. Hand compiling C is actually really easy. So you can stick to a subset of C (Just the atomic operations, the one that take a single CPU instructions) and have a very good idea of what's happening under the hood.
@@sebastiangudino9377 yeah thanks, I did learn C and C++ during my Computer Science degree in university years ago, so it's not that I don't know the fundamentals it's more about being familiar or effective in day to day use. What I meant by sticking with high level is because I actually don't want to really think about memory allocation, references etc and just like to work in higher level abstractions :P but ja, Rust started to feel like I needed to know more. So I still want to get into it again, but haven't :)
@@jamesbarrow Yeah! Going low level is just a performance thing. Since other ways there are just expensive allocations and copies EVERYWHERE. But to be fair. If you don't have performance needs you might just not need rust. I personally think Rust is beautifully elegant, but regular old C# can do the job really well
@@sebastiangudino9377 true. I do a lot of web dev, so the traction it's gaining along with Go in the JS tooling communities is making me interested at looking at it again ;)
Nice video; thanks! One small critique: 23:13 - Hmm, I think it's unfortunate that you not only delete s.clear() here, but also the println that comes after it... which would have worked. Might also have been a chance to show how you could make word live on, e.g. with a shadowing let word = String::from(word) (or maybe there are other ways to do that, too?). And then you kinda move on at 24:11, without ever showing a way to get what was presumably the intended goal of 22:49. Anyway, otherwise a helpful video, so thanks again!
Hey, amazing video, I have just something that I'm not sure of, At 23:20, after saying that we cant return reference of value in a function, you show a valid function (first_word), that returns a reference to str, is it because str is a special case ?
@13:37 I started to get confused between pass by reference and borrowing in rust. Is it the same thing or different. Initially when passing a `String` into a function, that function will take ownership of that `String`, meaning the original pointer to that `String` will get deleted after the function goes out of scope. When we declare a function to take a parameter by reference we use `&` symbol in front of the data type. Also , we prefix that symbol `&` when we pass a pointer. If we do not prefix it, when calling the function, it will tell to consider borrowing here. I start to think that borrowing is actually creating a new copy of pointer that points to the same data, after the function exits, the copied pointer will get deleted but not the original pointer and thus , the function does not take ownwership over the original pointer, instead it takes ownership of the second pointer being created, and thus the term borrowing ?
Can anyone please teach me at 5:50 when Bogdan said "x is a string literal which is store in the binary". Where is binary? So it is also out of stack? Thanks in advance
Good question. The binary is the file object created at compile time in a compiled language. So values established at compile time can be stored in the binary file and used quickly at run time, from automatic storage(stack).
Your vscode tells you when there is an error in the program like at 10:48, I have installed rust and rust-analyzer but I don't have those highlights when a error is there, is something else needed?
Hey! Love the videos, thanks so much for producing and sharing these! Just wanted to let you know that the flag in the background is backwards. Should always be displayed with the starts in the top left, even when displayed vertically!
Yes, the modern c++ idiom is to avoid raw pointers and use raii and smart pointers for any object needing ownership. It's probably because they are communicating to people who don't know the difference between c and c++ so explaining those differences takes a backseat to categorizing the languages broadly. I agree with your point.
First of all, thanks a lot for the tutorials! So much easier to learn from your vids than from the actual book. Now, I wanted to ask (at 17:04), what if I wanted to use r1 or r2 after the println!() statement? How would we approach that?
It will still work as long as you don't use any of them AFTER you declare r3. This is the point where the mutable reference is created and for it to work you must not have any reference (mutable or not) until r3 goes out of scope.
Hey, as an intermediate c++, programmer, your statement that accessing stack is faster than heap confuses me, heap is only slower in allocation, access time would be no different(I believe), stack might feel faster due to the fact most things in it would probably be cached in the cpu because we're using it a lot and when you try to get a value from a variable the cpu gets a chunk of memory from there instead of a single variable.(tho I'm not very educated in the cpu cache area, just my speculation)
I would guess that it is a simplification, to avoid having to get into the specifics of how memory allocations can keep happening, without the programmers control, after the initial allocation. Data structures can reallocate as could other things in the program. If there's no allocation to begin with it closes the door on these additional possibly hidden allocations that may occur at run time.
Man thank you - I have to use rust for a project and learning rust through the docs/book along with your video course has helped me a lot! You explain things really well and at a nice talking pace with examples and implementation - thanks bro!
Thanks for the video. I would like to know how to apply the slice and ownership for the array of String objects (not &str), especially when passing them to a function as arguments ? Thanks
I do feel you should add an asterisk behind "Slower write time" for the Ownership Model. For a beginner it's definitely (frustratingly) slower as, indeed, you have to fight the borrow checker. But I would thing that a very experienced Rust programmer "dances" with the borrow checker rather that fight with it. As a result, the struggle will be neglectable. Such a programmer will find it slower to write similar code with the same robustness in C or C++ as he would suddenly have to to worry about all the leaks and race issues.
also "slower write time" is extremely misleading. if would take the exact same time in C/C++ to write the exact same safe code. on the other hand, what you can code in less time in C/C++ is probably WRONG and riddled with bugs
Just a "linguistic" trick which may help beginners: Instead of using the word `reference`, prefer the word `borrow`. But... why? `Reference` is a noun which describes a certain thing whilst `borrow` is a verb which describes a certain action. The verb `borrow` is far more meaningful and elusive because `borrow` describes that a function borrows a variable from some outer scope, not taking ownership of it, just borrowing it. This mouthful of meaning once built in our brains is triggered everytime we hear the word `borrow`... so, let's use it! It's a reeducation process to ourselves, which I did myself. It's like: instead of saying "the function takes a reference to a String", we say "the function borrows a String". It's far more meaningful, since `borrow` suggests that we are going to use the variable for some time, return it back to the caller (cos we borrowed it, isn't it?), without taking ownership of it.
(Garbage,manual,ownership) all three are ways to free memory. Garbage collection means automatic memory management. The ownership-model is garbage collection on code pre-constrained by the borrow checker.
I genuinely don't understand that claim. I might get that you'd 'appreciate it more' not having to deal with either garbage collection/erroneous manual memory management, but not having C/C++ being a prerequisite to learn rust
Great video. Helped clarify things for me on how this works. Do you guidance on programming styles you recommend when it comes to moving/copying/etc? E.g. when to implement methods as references vs. copies, etc. Side note, the return of the first word function was s.len(), not i. Wouldn't the correct implementation of that return i?
Great video. I've been trying to watch more than a couple of times and read the book to fully understand why some types are made a copy of by default and some get's ownership transfered? What was the feature in mind?
Great video series! I really like your videos. One question though - I know it might seem a bit picky. During your stack & heap explanation you say, for method b() the value of var x gets stored in the heap. However it is not a "mut" - wouldn't this apply to mutable variables only or is it based on the type (string in this case)? Thanks in advance and keep going.
📝Get your *FREE Rust Cheatsheet* : www.letsgetrusty.com/cheatsheet
As a line-of-business developer I've been a bit intimidated about learning an innovative system language like Rust - but this makes one of the gnarliest features seem learnable. You're a good teacher.
Wait until u learn traits
@@stardustbiscuits so crazy true
I don’t know why, but the borrow checker doesn’t confuse me that much. Use a reference when you want to see the original memory through a window, move when you want the data for yourself, and never have a window to a place that does not exist.
You are one of the chosen.
a good example from soft engg is to compare the ownership model with read-write lock semantics...
The basics of borrow checking isn't hard, but sometimes it can get tricky and very confusing in complicated code
This is the best concise explanation of borrowing I've seen yet
@@ddastoor
because it's about the same problem
I am going to watch this every day until I understand it in my bones
REAL
Finally, found someone that covers the rust guide book. Thanks, man. Super time saver
Glad I could help!
You're an incredible teacher, so much clarity.
He doesn't derail ever, seamless additive commentary, and an enjoyable voice haha
+1
Most of what he said about stacks and heaps was misleading enough that I don't think he really understands it very well.
Basically just summarizing the book
@@-karter-4556 almost word for word plagiarism
This is by far the best explanation about Rust's borrow mechanism I've encountered so far! The course is an excellent resource for Rust newcomers. Great work!
10:53 "When we pass in parameters into a function its the same as if we were to assign s into another variable" - now that's when the borrow checker finally clicked for me. Now I also understand why it's so controversial to some people. Your tutorial so clear and easy to understand. Thank you!
im amazed at how much the rust-analyzer and compiler are teaching me without even running code. You're explanation makes it a top-grade experience
I think that was the best explanation I've come across so far...you took the time a noobie would need to learn this stuff, and didn't try to "keep under two minutes" destroying the clarity for the sake of speed. Also, you didn't jump immediately to metaphors of "oh, its like if you have a book, and you lend it to someone, but that person can write on the book" and so on...I also find those unnecessary and confusing sometimes
7:57 - why would you have to use new to allocate memory on the heap in C++? Just use std::string s("hello"), or std::vector and it works just like it did since 30 something years, memory is deallocated at the end of scope.
True, you don't have to manage the memory yourself with raii data structures. Be aware these data structures are still doing dynamic allocation, but they are doing it in a safe way. You still take a potential performance hit but memory leaks shouldn't be an issue.
You can use a std::array if you want to avoid the free store and use automatic(commonly referred to as stack) storage. Std::array requires a size known at compile time.
This is really fantastic! Your cadence, examples and explanations are really great! I've been programming for 25 years (C++, C#, Js, etc etc) and this is a really nice way to understand nuances of rust. Thank you!
25 years and this rank amateur explains things well? What? Are you one of those php script monkeys? It was awful.
So I recently just made my first foray into Rust by attempting to build a calculator in a Yew app. I am still struggling with the logic itself, but I actually found the battle with the borrow checker to be one of the more refreshing sorts of problems I ran into. It made me think so much harder about where I was declaring my variables and where I was mutating them that it kinda just felt like my brain was steadily increasing in mass and wrinkle count the whole time.
this is how I sort of interpreted it.."stop doing these things unless you really need to because they're just problematic"
I am new to Rust, read ownership chapter twice, got the concepts of ownership, reference and moves but still wasn't confident. This video did the trick and made everything crystal clear. Thanks a ton!
I read the book and i was overwhelmed of new terms and information. this video helped me to visualize it live. yes rust book visualizations were great but for me i find this more helpful. i think after watching this, i will understand the book better. thank you.
there are too little learning resources for rust :)
I have watched tilll 17:19 and can say the explanation is top notch took me 45 min to react this point as I am coding as well the information which I find useful will continue from here the next day
Very nice series
As a hobbyist who's dabbled in a bunch of languages because it's fun, i'm now learning me some rust.
Certainly doing my share of fighting with the borrow checker but that said, i'm super impressed with the errors and warnings that the compiler spits out.
Most helpful messages that i've encountered in any language; have helped me sort out a bunch of things which in other languages i would have had to fire up the google to work out what was going on.
I think this is the best video on RUclips to explain the ownership model. Great Work.
Video form of the book helps me so much, I read really slow, you covered the entire chapter in 25 min shorter than I could probably read it lol.
I was worried learning Rust, but more I look at this. THIS MAKES SO MUCH SENSE!
17:16 is such an elegant info. I love how they designed Rust language
I read the rust book ownership chapter but was confused. Your video makes the concept much clearer. Thanks and keep it up!
First tutorial series on RUclips where I don't even hesitate for a second before clicking the thumbs up button. Great job!
Wish I had this video last year when I was learning Rust. You explained the concepts fantastically.
Really good video. I'll be coming back to this over and over until it's stuck in my memory.
Mindblowing 🤯🤯🤯
Thank you so much for your dedication. You're an incredible teacher!
the reference and borrow is definingly the best part of this tutorial
@ 17:14 how are r1 and r2 out of scope after line 7 ?
what if I want to use them after the print stmt ?
This is a bit old, but in case somebody else is with the same question, it is because in the context of the code he is presenting, the lifetime of r1 and r2 are implicitly tied to the last time they are used. They were dropped to make the definition of mutable reference r3 possible.
Honestly man, your videos have really helped me whilst I go through the book. Theres a lot of information to consume so appreciate you taking the time to make these accompanying videos (y), some things are easier to see than to read and vice versa :)
Right around 14:10 was the lightbulb moment for me. And being a long time C and C++ programmer I think I'm finally starting to see why this Rust thing makes sense... What a clever way to make sure that we know who is writing data and who is just reading or calling getters. It makes the C/C++ way of using references, pointers and const seem kind of silly.
The very best channel for Rust learners that I have found so far! Thank you, buddy! Wish you all the best and prosperity to your channel!
10:53 gold info here. love this channel
i took an online course, it was short but still, it explained Strings and string slices. (literals) etc... but the way you explained them in this video were so much more clear. thank you.
This was the best outline of this topic that I’ve seen. Thank you.
what a great explanation I was so confused now that I decided to learn the language and you made it all clear for me in 25min YOU ROCKS! thx from 🇧🇷
I was struggling understanding Steve Klabnik's and Carol Nichols' book, but you made it perfectly clear. Thanks!
I work with node and go for my serious business projects but I did get a offer from a firm that uses rust and pretty flexible timeline if I ever wanted to onboard. This got me into going through the rust book and learning the language… I really like it! I also liked go a lot too.. probably because I started in insane crazy js land now these relatively new languages seem to nice
What I really like about your videos, until now :-), is the speed, you tell things. I'm not native American or English, but most every tutorial I watched for instance on Udemy was so slow. Of cause someone can speedup the video, but sometimes, the speaker speaks faster or changes slides, and that results in confusion.
Very, very well done, again until here 🙂as I don't know more of your videos besides the seen ones.
This is the best explanation,
I have come across.
Best explanation about ownership and borrowing, it helped me a lot.
I wish I found this language sooner 😫. The control and defined, predictable behavior is so appealing.
You sir... are an incredible teacher and have just got a new subscriber!
Man I'm getting all worked up converting my c++ program to rust. 2 days in and I am no where near to finish it. Now I've realized that my "c++ way of thinking" is getting in the way.
I have just started learning Rust and your videos are helping me to understand all the tricky Rust concepts. Thanks!
.
You're an amazing teacher thanks man. The way you expained how rust stops two mutable references in the same scope to prevent race conditions. If I am not wrong, this feature isnt there in golang, and this is where rust outshines. This feature will outshine even more, when concurrency comes into play. Explaining why rust promotes safe concurrency. Correct me if I got this right.
Coming from c++ this is intuitive. Unique pointers and move semantics give this type of behavior as options in modern c++ so having them as the default makes sense. Also having the const as default on refs is another good safety measure. I do think a basic understanding of pointers in c and references and smart pointers in c++ will help people understandownership and the ideas it's built upon.
Thank you for making this. It's so helpful and so easy to follow along. You're amazing
Going through the rust book right now... very helpful!
Perfect explanation, even though the concept is hard to understand, the explanations and examples provided in this video are very valuable. Thank you for it.
This video is incredible, I can’t believe you don’t have more subs
Awesome teaching. The pace is very good and information goes straight into my brain with good understanding of the concept.
This channel is pure gold, thanks man!
FINALLY SOMEONE WHO DO STUFF IN CODE INSTEAD OF TALKING IN AIR :D
Thanks for the video. I have a question about the code example in the "Stack & Heap" section:
- Is there any reason why a() and b() are defined within main() scope rather than, more commonly, outside main()?
- a() is actually never called by main() and therefore neither is b(). Do the stack frames get constructed anyway as in the picture?
No particular reason for a() and b() being defined inside main(). Stack frames will not be created unless the functions are called so my example code isn't 100% accurate. Good eye!
Ah.. i asked the same question... good reply.. 👍
You explained it in a very concise and clear way. Good job!
Hey mate! Keep going! Very well explained.
Thanks Gleb!
*Me* : Casually starts to watch video to understand closures
After sometime , Can't stop watching other videos.
I was so deeply involved in listening your videos, I noticed a background music, could you please tell me which song it is :D
for my new job i have to learn rust fast and i'm not really good at reading documentations and books so thank you for great and useful videos
I read the Rust book a couple of years ago now but never really made use of Rust because all the borrow checking and lower level things made me realize maybe I'm happier staying with C# or some higher level language. I've been wanting to get back into it, or maybe to learn Go. Rust seems quite popular these days so starting to go that way again. Your videos are very well presented, thank you for taking your time to share your knowledge in such a clear and calm way, it's very motivating, much appreciated
If you are already familiar with C#. I would actually recommend you learn a LITTLE bit of C++ before. The Syntax is kinda similar (They are oriented around OOP), but C# has a Garbage Collector, while C++ has only manual memory Management. So you'll learn about pointers, malloc and new, destructors, smart_ptrs, references (to lvalues and rvalues), copy constructor and move semantics.
Those are things, C# does not have. So you can take like a day to learn the Syntax of C++ (How to do in C++ what you can do in C#) and then learn the memory management stuff which is the hard part.
That way you can understand how hard and unreliable this stuff really is, and how rust, using the same abstractions (But with the added concept of borrowing (A type of move) that can be tracked by the borrow checker) can solve the same issues of C++, and a lot more, by default at compile time. And all the weird anoying part of the languages that you struggled with will probably now make sense as a logical next step, or a consequence of the things that you now know are happening under the hood!
You could also just keep going. Rust is s logical simplification of C++. But C++ is just a superset of C created to simplify reasoning about objects instead of just bytes in memory, and as such allowing things like RAII which are imposible in C.
And C is just a logical next step above assembly. Hand compiling C is actually really easy. So you can stick to a subset of C (Just the atomic operations, the one that take a single CPU instructions) and have a very good idea of what's happening under the hood.
@@sebastiangudino9377 yeah thanks, I did learn C and C++ during my Computer Science degree in university years ago, so it's not that I don't know the fundamentals it's more about being familiar or effective in day to day use. What I meant by sticking with high level is because I actually don't want to really think about memory allocation, references etc and just like to work in higher level abstractions :P but ja, Rust started to feel like I needed to know more. So I still want to get into it again, but haven't :)
@@jamesbarrow Yeah! Going low level is just a performance thing. Since other ways there are just expensive allocations and copies EVERYWHERE. But to be fair. If you don't have performance needs you might just not need rust. I personally think Rust is beautifully elegant, but regular old C# can do the job really well
@@sebastiangudino9377 true. I do a lot of web dev, so the traction it's gaining along with Go in the JS tooling communities is making me interested at looking at it again ;)
Nice video; thanks! One small critique:
23:13 - Hmm, I think it's unfortunate that you not only delete s.clear() here, but also the println that comes after it... which would have worked. Might also have been a chance to show how you could make word live on, e.g. with a shadowing let word = String::from(word) (or maybe there are other ways to do that, too?). And then you kinda move on at 24:11, without ever showing a way to get what was presumably the intended goal of 22:49.
Anyway, otherwise a helpful video, so thanks again!
Beautiful video bro. Thanks for creating this. I understand what is so special about Rust now
Hey, amazing video, I have just something that I'm not sure of, At 23:20, after saying that we cant return reference of value in a function, you show a valid function (first_word), that returns a reference to str, is it because str is a special case ?
Best video on rust ownership
awsome explanation, keep up the good work
@13:37 I started to get confused between pass by reference and borrowing in rust. Is it the same thing or different.
Initially when passing a `String` into a function, that function will take ownership of that `String`, meaning the original pointer to that `String` will get deleted after the function goes out of scope.
When we declare a function to take a parameter by reference we use `&` symbol in front of the data type. Also , we prefix that symbol `&` when we pass a pointer. If we do not prefix it, when calling the function, it will tell to consider borrowing here.
I start to think that borrowing is actually creating a new copy of pointer that points to the same data, after the function exits, the copied pointer will get deleted but not the original pointer and thus , the function does not take ownwership over the original pointer, instead it takes ownership of the second pointer being created, and thus the term borrowing ?
In a nutshell `&` is used to prevent function or for loop to take ownership of data on the heap from a pointer on the stack.
Can anyone please teach me at 5:50 when Bogdan said "x is a string literal which is store in the binary". Where is binary? So it is also out of stack?
Thanks in advance
Good question. The binary is the file object created at compile time in a compiled language. So values established at compile time can be stored in the binary file and used quickly at run time, from automatic storage(stack).
In the c environment the binary is a .o file. Not sure in rust as just looking into this.
Thank you@@fennecfox2366
Thank you for the wonderful explanation. Extremely valuable
good work ! I understand a lot ! can't wait to see more Rust content !
around 6:12, your explaining the stack & heap, but the code example does not show how fn a() {... is executed first? Someone help 🙏
Great vid, looking forward to watching the rest of them!
Thank you so much, I am learning rust after java and javascript background, and it's quit tough. But your videos help a lot! Thank you
Your vscode tells you when there is an error in the program like at 10:48, I have installed rust and rust-analyzer but I don't have those highlights when a error is there, is something else needed?
Thanks a lot for this series. Very good explained.
When I watched 2 hour tutorial for rust and was completly confused but now its like really easy
You are a machine gun of free and high quality knowledge wow
Brilliant explanation. Thank you.
Hey! Love the videos, thanks so much for producing and sharing these!
Just wanted to let you know that the flag in the background is backwards. Should always be displayed with the starts in the top left, even when displayed vertically!
22:30 what does b' ' mean? i get the ' ' but not the "b"
Thank you for explaining stack and heap. Now Rust makes more sense in terms of making some of the code design pattern decisions.
I have a question. The shared_ptr template has been in C++ since 2003. Why do people still claim that objects are freed manually using delete?
Yes, the modern c++ idiom is to avoid raw pointers and use raii and smart pointers for any object needing ownership. It's probably because they are communicating to people who don't know the difference between c and c++ so explaining those differences takes a backseat to categorizing the languages broadly. I agree with your point.
Realy good explanation. TY!
Thanks! Very good explanation!
First of all, thanks a lot for the tutorials! So much easier to learn from your vids than from the actual book.
Now, I wanted to ask (at 17:04), what if I wanted to use r1 or r2 after the println!() statement? How would we approach that?
It will still work as long as you don't use any of them AFTER you declare r3. This is the point where the mutable reference is created and for it to work you must not have any reference (mutable or not) until r3 goes out of scope.
Thanks! Very understandable video. Easy to learn.
Save me time reading thru the chapter myself 😄
Hey, as an intermediate c++, programmer, your statement that accessing stack is faster than heap confuses me, heap is only slower in allocation, access time would be no different(I believe), stack might feel faster due to the fact most things in it would probably be cached in the cpu because we're using it a lot and when you try to get a value from a variable the cpu gets a chunk of memory from there instead of a single variable.(tho I'm not very educated in the cpu cache area, just my speculation)
I would guess that it is a simplification, to avoid having to get into the specifics of how memory allocations can keep happening, without the programmers control, after the initial allocation. Data structures can reallocate as could other things in the program. If there's no allocation to begin with it closes the door on these additional possibly hidden allocations that may occur at run time.
Man thank you - I have to use rust for a project and learning rust through the docs/book along with your video course has helped me a lot! You explain things really well and at a nice talking pace with examples and implementation - thanks bro!
This lesson was amazing, thank you very much 🦀❤️
Thanks for the video. I would like to know how to apply the slice and ownership for the array of String objects (not &str), especially when passing them to a function as arguments ? Thanks
I love this videos. Great work!
I do feel you should add an asterisk behind "Slower write time" for the Ownership Model. For a beginner it's definitely (frustratingly) slower as, indeed, you have to fight the borrow checker. But I would thing that a very experienced Rust programmer "dances" with the borrow checker rather that fight with it. As a result, the struggle will be neglectable. Such a programmer will find it slower to write similar code with the same robustness in C or C++ as he would suddenly have to to worry about all the leaks and race issues.
also "slower write time" is extremely misleading. if would take the exact same time in C/C++ to write the exact same safe code. on the other hand, what you can code in less time in C/C++ is probably WRONG and riddled with bugs
Idk who invented the borrowing/ownership concept, but it's genius.
Just a "linguistic" trick which may help beginners: Instead of using the word `reference`, prefer the word `borrow`. But... why?
`Reference` is a noun which describes a certain thing whilst `borrow` is a verb which describes a certain action.
The verb `borrow` is far more meaningful and elusive because `borrow` describes that a function borrows a variable from some outer scope, not taking ownership of it, just borrowing it. This mouthful of meaning once built in our brains is triggered everytime we hear the word `borrow`... so, let's use it!
It's a reeducation process to ourselves, which I did myself. It's like: instead of saying "the function takes a reference to a String", we say "the function borrows a String". It's far more meaningful, since `borrow` suggests that we are going to use the variable for some time, return it back to the caller (cos we borrowed it, isn't it?), without taking ownership of it.
Very interesting point, thanks for sharing!
@@letsgetrusty Videos 18-22 of your playlist are private. Just curious to see what they are about. Thanks :-)
@@richardgomes5420 Should be fixed now!
Indeed very interesting point, it makes easier to build the logic in ours heads by just changing the word from reference to borrow
(Garbage,manual,ownership) all three are ways to free memory. Garbage collection means automatic memory management. The ownership-model is garbage collection on code pre-constrained by the borrow checker.
This is BY FAR the hardest part to learn Rust. It's frustrating to learn without any C background tbh.
I genuinely don't understand that claim. I might get that you'd 'appreciate it more' not having to deal with either garbage collection/erroneous manual memory management, but not having C/C++ being a prerequisite to learn rust
Great video. Helped clarify things for me on how this works. Do you guidance on programming styles you recommend when it comes to moving/copying/etc? E.g. when to implement methods as references vs. copies, etc.
Side note, the return of the first word function was s.len(), not i. Wouldn't the correct implementation of that return i?
Great video. I've been trying to watch more than a couple of times and read the book to fully understand why some types are made a copy of by default and some get's ownership transfered? What was the feature in mind?
finally learning rust 💙
This video was great. Thank you 👍
Great video series! I really like your videos. One question though - I know it might seem a bit picky.
During your stack & heap explanation you say, for method b() the value of var x gets stored in the heap. However it is not a "mut" - wouldn't this apply to mutable variables only or is it based on the type (string in this case)?
Thanks in advance and keep going.
It is based on the type.