Rust Demystified 🪄 Simplifying The Toughest Parts

  Рет қаралды 192,256

Code to the Moon

Code to the Moon

Күн бұрын

Пікірлер: 605
@codetothemoon
@codetothemoon 2 жыл бұрын
ERRATA * I mention that you get a segfault when you don't deallocate memory in C/C++ - I meant to say that you get a segfault when you try to reference memory that was already deallocated. Not deallocating memory will lead to memory leaks in some cases.
@rayahhhmed
@rayahhhmed 2 жыл бұрын
nah still,, a banger video
@codetothemoon
@codetothemoon 2 жыл бұрын
@@rayahhhmed thanks!
@AGBuzz182
@AGBuzz182 2 жыл бұрын
It's not generally true that accessing deallocated memory causes a segfault either. Actually, it would be more helpful if that was the case, since it would be easier to debug than the case where your program is silently accessing data that may or may not be garbage depending on the execution state.
@MikaelMurstam
@MikaelMurstam 2 жыл бұрын
dammit I was gonna yell at you :)
@patricklittle1002
@patricklittle1002 2 жыл бұрын
Many embedded systems just use static allocation, so you never deallocate by design.
@cathalogrady2331
@cathalogrady2331 2 жыл бұрын
you dont get a segfault if you dont deallocate memory. Segfaults are when the kernel informs your process that you accesed memory you shouldnt have
@codetothemoon
@codetothemoon 2 жыл бұрын
ugh, you're absolutely right! This was an egregious mistake. Not deallocating will result in a memory leak. I meant to refer to the case where we erroneously deallocate memory and then try to reference that memory. I'll put a note in the description, thanks for pointing this out!
@GottZ
@GottZ 2 жыл бұрын
yo ucan also segfault by accessing memory you never had access to. (in most cases..)
@cathalogrady2331
@cathalogrady2331 2 жыл бұрын
@@GottZ yes I never said that wasnt the caes, I said purely deallocation alone doesnt provoke a segfault directly
@pinealservo
@pinealservo 2 жыл бұрын
@@codetothemoon Also, you can segfault if you correctly deallocate memory and then (because you had more than one reference) you deallocate it again and corrupt the malloc recordkeeping. Sometimes this takes quite a while to blow up on you!
@hotpaws_maths_and_science
@hotpaws_maths_and_science 2 жыл бұрын
you segfault every time you run your c program for the first time
@abraham7966
@abraham7966 2 жыл бұрын
I like what you are doing here. I am glad that No Boilerplate is influencing people because I am sick of all the stupid presentations in other channels. 1 minute of BS, songs, animations, and people presenting their channels and welcoming as if we were 6 years old watching Ryan's toys reviews.
@codetothemoon
@codetothemoon 2 жыл бұрын
Thanks! re: the filler stuff - yeah I'm not a fan of lengthy introductions either. I'm not entirely above obnoxious animations (see earlier videos) but I definitely like to get to the point 😎
@megumin4625
@megumin4625 2 жыл бұрын
7:44 In case anyone is wondering why the mutable reference will work if you remove the last `print_some_struct` which uses an immutable reference. This is because of NLL (non-lexical lifetimes). In short, the compiler infers that it is able to drop the immutable reference borrow before it gets to the mutable reference, because it's not used anywhere later. Thus, you only have one mutable reference, which doesn't break any rules
@robonator2945
@robonator2945 Жыл бұрын
Great, as if Rust hadn't gone out of it's way to make things confusing already they don't have NULL, they have NLL. I'm half convinced this is intentional at this point and this is all just some cruel trick by some trickster diety of programming to make an incredible language that is intentionally designed to drive as many people as possible away from using it. Wait... trickster diety, Loki - Odin lang, Odin... checkmate athiests.
@z4nshin
@z4nshin 8 ай бұрын
It's good you mentioned this. This confused me for a long time.
@lukakralik2346
@lukakralik2346 2 жыл бұрын
I was learning Rust for some time now. I already understood borrowing and the entire ownership model really well but didn't admit I don't get lifetimes at all. Today it finally cliked for me (after 3 months of learning rust :D). It was so obvious and under my nose the entire time! Thank you very much for your amazing explanation! I am incredibly excited to finally dive into Rust completely.
@codetothemoon
@codetothemoon 2 жыл бұрын
Nice, glad you found the lifetime explanation helpful! I wasn't 100% sure if my explanation was as straightforward as possible, so this anecdote makes me very happy.
@brunkel8269
@brunkel8269 2 жыл бұрын
Your videos on Rust are well explained and to the point. Plus with the production quality of these video's, it will only take a matter of time before your channel blows up!
@codetothemoon
@codetothemoon 2 жыл бұрын
Thanks for the kind words Brunkel! I aim to make videos that are engaging but still pack in as much value as possible. I'd love to do this as a full time job, I appreciate you watching as it really helps me toward that goal!
@antoniofornie
@antoniofornie 2 ай бұрын
No offense to other youtubers, but this is by far the best explanation on this topic. Specially when it comes to lifetimes. Thanks!
@codetothemoon
@codetothemoon 2 ай бұрын
thank you so much for the kind words!
@andythedishwasher1117
@andythedishwasher1117 2 жыл бұрын
I just started learning Rust a couple weeks ago and decided to build a calculator in a Yew app. Finally got it up and running last night. I was excited about the ownership concept for similar reasons to what you described due to enormous frustration with trying to rebuild Javascript after encountering runtime errors on runtimes I couldn't test in the development phase. Turns out, it made the calculator thing easier rather than harder by some miracle. I did run into some pretty confusing lifetime errors though. You have to be super careful where you declare things and how long you keep them around, but if you can do that, you're pretty much gold. The whole process for building and deploying a Yew app was a fair bit more enjoyable than React as well. Definitely nicer than wrestling with node_modules.
@codetothemoon
@codetothemoon 2 жыл бұрын
Nice Andy! I love hearing stories like this. I imagine others will have similar experiences
@InMemoryOfNeo
@InMemoryOfNeo 4 ай бұрын
I watched this video about 1 year ago but I didn't understand the lifetime specifier thing at the time. But after 1 year I investigated deeply and asked questions in rust forum and now I completely understand it. Thanks.
@mdshohidurrahman1121
@mdshohidurrahman1121 2 жыл бұрын
Every new rust developer should watch this video,very effective as always.
@codetothemoon
@codetothemoon 2 жыл бұрын
Thanks! I really do hope this can be a resource for newcomers that removes as much of the friction as possible.
@emiliorojas583
@emiliorojas583 2 жыл бұрын
@@codetothemoon it really does XD. I'm eager to learn rust and this video made me more interested. this video is pure gold. from the bottom of my heart: thanks you
@cracktek_industries
@cracktek_industries 5 ай бұрын
You're the only person on youtube who actually made me understand lifetimes. Thanks!
@codetothemoon
@codetothemoon 5 ай бұрын
Great! Really happy you got value out of it!
@thisoldproperty
@thisoldproperty 2 жыл бұрын
This was my second rust video I’ve watched and as a senior coder even I’m amazed at the level of thought that has gone into the language.
@codetothemoon
@codetothemoon 2 жыл бұрын
I agree! As with any language, there are some aspects I don't like but it's really incredible what the language has accomplished!
@Chuukwudi
@Chuukwudi 2 ай бұрын
Quite dense and fast. I need to rewatch multiple times to let this digest. Although it is not easy, I think It is a good thing. Thanks for the wonderful videos.
@codetothemoon
@codetothemoon 2 ай бұрын
thanks, glad you got something out of it!
@voidemperor9372
@voidemperor9372 2 жыл бұрын
Magicccc!! we need more of these short videos. Great job :)
@codetothemoon
@codetothemoon 2 жыл бұрын
Thanks void, more to come!
@ItsGazareth
@ItsGazareth 2 жыл бұрын
This was great, I love how you gave actual examples for the things the compiler was complaining about.
@codetothemoon
@codetothemoon 2 жыл бұрын
Thanks Gazareth, glad you found it valuable!
@gladoseus
@gladoseus 10 ай бұрын
Very clear explanations and straight to the point. This the best 14 minutes of my rust journey so far.
@codetothemoon
@codetothemoon 10 ай бұрын
thank you, glad you got something out of it!
@Dygear
@Dygear Күн бұрын
Came back to say this is probably the best explanation of lifetimes I've seen. Thank you for making this video, it is ever green content.
@LukasRotermund
@LukasRotermund 8 ай бұрын
Wow, this is a brilliant video! I had some trouble with the borrowing, but now I've got it, thanks to you! 💡
@codetothemoon
@codetothemoon 8 ай бұрын
fantastic, really happy this particular approach made things clearer for you!
@user-vs9uf3ny8z
@user-vs9uf3ny8z Жыл бұрын
I definitely got tripped up when first encountering the syntax and explanations around this in official docs and other resources. You really broke things down in a beautifully comprehensible way. Thanks so much for the vid!
@codetothemoon
@codetothemoon Жыл бұрын
thanks, really happy you liked it!
@mrhoxell
@mrhoxell 9 ай бұрын
Very good tutorial. I had trouble with moving playing with Rust and despite understanding somehow how it's working, this explains the basic concepts very well. Thanks a lot
@codetothemoon
@codetothemoon 9 ай бұрын
nice, really happy it was helpful!
@TheRealAfroRick
@TheRealAfroRick Жыл бұрын
Nice. Really love how clearly you explained the concepts - especially the WHY as that makes it easier to peer behind the compiler and understand what's happening - ESPECIALLY with Lifetimes. I'll have to watch a few more times for it to burn into long term memory, but this is the first time I've understood lifetimes 😂
@codetothemoon
@codetothemoon Жыл бұрын
thank you, really happy you got something out of it! i felt like so many people get stuck on these concepts, and there was an easier way of approaching them...
@dazealex
@dazealex Жыл бұрын
I've watched many channels, but yours is by far the best explanation style I've seen so far! Kudos bro.
@smashingtonn
@smashingtonn 6 ай бұрын
This was very clear and useful. Best tutorial for rust I've found
@codetothemoon
@codetothemoon 6 ай бұрын
thank you, really happy you got value out of it!
@mbrav
@mbrav 2 жыл бұрын
I have finished the Rust book along with rustlings exercises, which I cannot recommend enough to everyone who wants to learn Rust. But this video was so great at solidifying the concepts I learned! Please do more content like this! *subscribed*
@codetothemoon
@codetothemoon 2 жыл бұрын
The Rust Book is fantastic! More videos on the way. Very Happy to have you onboard!
@egorandreevich7830
@egorandreevich7830 Жыл бұрын
What's wrong with rustlings? I wanted to try it.
@mbrav
@mbrav Жыл бұрын
@@egorandreevich7830 Nothing, rustlings exercises are wonderful
@egorandreevich7830
@egorandreevich7830 Жыл бұрын
​@@mbrav so why you cannot recommend it?
@mbrav
@mbrav Жыл бұрын
@@egorandreevich7830 I cannot recommend it ENOUGH. Meaning is inversed.
@tobias3581
@tobias3581 2 жыл бұрын
Amazing just got fed up with JS for the eleventh time this week and started reading rust docs and now this, thanks!
@codetothemoon
@codetothemoon 2 жыл бұрын
Thanks Tobias, I really appreciate you watching each video!
@achimwasp
@achimwasp Жыл бұрын
Shows why I like languages with GC so much. (defn bigger [a b] (if (> a b) a b)) (bigger 3 5) Done. Depends of course on which you prefer or need for the use case: developer performance or code/app performance.
@ИльяПрохоров-б9в
@ИльяПрохоров-б9в 13 күн бұрын
"a and b need to stay in scope at least as the return value in this function" very good explanation
@codetothemoon
@codetothemoon 9 күн бұрын
really glad this perspective helped!
@QazCetelic
@QazCetelic 2 жыл бұрын
These were indeed exactly the parts that were hard to get used to as someone who is used to GC collected languages. Very useful tutorial.
@codetothemoon
@codetothemoon 2 жыл бұрын
Thanks Qaz, glad you found it valuable!
@jonathanlevi2458
@jonathanlevi2458 2 жыл бұрын
Thanks!
@codetothemoon
@codetothemoon 2 жыл бұрын
wow thank you so much Jonathan! It's hard for me to fully articulate how encouraging it is to see support like this 😎
@richsadowsky8580
@richsadowsky8580 2 жыл бұрын
Another great video. Lifetimes have been giving me grief and this helped. With 40 years experience programming in at least a dozen languages, these really are the somewhat unique and challenging bits of Rust.
@codetothemoon
@codetothemoon 2 жыл бұрын
Thanks Rich, really happy you found it helpful!
@TheArgentan
@TheArgentan 2 жыл бұрын
Great video! I really feel like I've understood borrowing and lifetimes for the first time since the first time I was rust-curious a year ago
@codetothemoon
@codetothemoon 2 жыл бұрын
Thanks Pat, really happy this helped clear up the confusion around these concepts!
@Someone-q6f5x
@Someone-q6f5x 2 жыл бұрын
This channel is a blessing
@codetothemoon
@codetothemoon 2 жыл бұрын
Very happy to have you onboard!
@soumen_pradhan
@soumen_pradhan 2 жыл бұрын
Would like to see one on Procedural Macros as well. They are essentially magic to me right now.
@codetothemoon
@codetothemoon 2 жыл бұрын
Great idea Soumen! In fact I have this in the works already, it might be the next video.
@marl3x
@marl3x 2 жыл бұрын
Macros are the reason I immediately stopped using Rust after learning it through their book. I knew how to code in Rust, wanted to build something and suddenly nothing made sense anymore because everything was obfuscated by these stupid magic Macros. But apparently that's just something you have to deal with, some libraries just don't want you to know what's really happening.
@erikfundercarstensen7344
@erikfundercarstensen7344 2 жыл бұрын
@@marl3x I think its ‘cargo expand’ which can be used for printing the result of macro expansion in a given program. I think the library developers are prioritizing usability and small code size over understandability, which sadly sometimes are tradeoffs
@SaHaRaSquad
@SaHaRaSquad 2 жыл бұрын
​@@marl3x I don't think I've ever used any third-party macros, only the ones from stdlib and my own. As Rust docs for libraries are auto-generated from code they contain everything either way, so you can usually avoid macros. I know some libraries separate their macros into another optional package as well.
@porky1118
@porky1118 2 жыл бұрын
@@marl3x I'm using Rust for years already and I almost never come across libraries, which use macros. I also don't use a lot of macros myself. Almost always, it's been macro specific libraries. Mostly custom derives. If one uses macros, it's not about not wanting the user to know, what's happening internally, it's a simplified syntax for a special purpose. But I also don't like, when I see a library, which forces me to use a lot of weird macros.
@introspecticon
@introspecticon 2 жыл бұрын
Great pacing and presentation - quickly becoming one of my favorite Rust channels
@codetothemoon
@codetothemoon 2 жыл бұрын
Thanks for the kind words! If there are any topics you'd like to see let me know!
@introspecticon
@introspecticon 2 жыл бұрын
@@codetothemoon these might be a bit niche, but here are a few things I'd like to learn more about in no particular order: - speeding up python code using PyO3 - high-performance/multithreaded data processing with ndarray and polars - speeding up a React/Svelte SPA by writing expensive business logic in Rust and compiling it to WASM - The current status of WASM/WASI and when to expect WASM to get better at DOM manipulation
@luiscarloscorderopena7030
@luiscarloscorderopena7030 2 жыл бұрын
Excelent explanation!, really loved it! it's very concise and to the point
@codetothemoon
@codetothemoon 2 жыл бұрын
Thanks Luis, glad you found it valuable!
@_dinesh
@_dinesh Жыл бұрын
Excellent explanation!!! keep doing more videos like this!!
@codetothemoon
@codetothemoon Жыл бұрын
thanks, more to come!
@eladmatia9953
@eladmatia9953 2 жыл бұрын
I think I finally understand lifetimes. Simple and elegant. Thank you!
@codetothemoon
@codetothemoon 2 жыл бұрын
Nice, glad you found the video helpful, thanks for watching!
@footballCartoon91
@footballCartoon91 Жыл бұрын
in rust, when instantiating a `struct` , on the stack, is the variable that we declare is actually a pointer in disguise which also points to data on the stack. like in @1:44. Because, here there is no mentioning about the data (i.e `SomeStruct`) is passed as a reference or by value to the function `print_some_struct`. What we all know by default the data is "moved" to the function, and so the pointer that is initialized earlier is invalid after the function exit (or return).
@KnightMirkoYo
@KnightMirkoYo Жыл бұрын
The borrow checker makes so much sense, it's surprising that it never got popular before rust. Rust makes me think about memory and types, and the compiler ensures I mostly make correct choices. It feels awesome.
@codetothemoon
@codetothemoon Жыл бұрын
I agree! it's interesting how many recent innovations in software (like the borrow checker) are completely independent of modern hardware and theoretically could have been discovered decades ago. Blockchain and Transformers are also great examples.
@jrtomsic
@jrtomsic Жыл бұрын
Thanks for this video. I tried rust for a while and basically just fumbled with references and lifetime definitions until it did what I want. This video does a good job showing what each of those concepts is so I can be deliberate about my code next time.
@codetothemoon
@codetothemoon Жыл бұрын
nice, really happy you got something out of the video! 😎
@torppe2226
@torppe2226 2 жыл бұрын
Incredibly easy to understand and straight to the point video, keep up the good work!
@codetothemoon
@codetothemoon 2 жыл бұрын
Thanks, glad you found it valuable!
@ItzKernel
@ItzKernel 2 жыл бұрын
this was a very helpful video. i already knew how to use lifetimes because i got used to them, but i couldn't have explained how they work. now everything is crystal clear. keep up the good work!
@codetothemoon
@codetothemoon 2 жыл бұрын
glad it was helpful, and thank you!
@jacoballen6099
@jacoballen6099 2 жыл бұрын
Killer video. Bet this guys channel blows up if he keeps making content in this style
@codetothemoon
@codetothemoon 2 жыл бұрын
Thanks Jacob, I hope you're right!
@youpie
@youpie 3 ай бұрын
what i dont get with the lifetimes is why, with the stuct, example. the compiler can't infer that the reference to the i32 has to live as long as the struct. It literally doesnt work otherwise
@notengonickname
@notengonickname 2 жыл бұрын
I started learning Rust a couple weeks ago and had a hard time grasping the concept of Lifetimes... which now I do because of your video! Great content 👌Thanks a lot !!!
@codetothemoon
@codetothemoon 2 жыл бұрын
Nice Jorge! Glad you found the video helpful!
@VishalPaudel-i6r
@VishalPaudel-i6r 9 ай бұрын
Thanks for all your explanatory videos on Rust. I am learning just as much I should from videos without the becoming tutorial dull. I had one question, which I later checked on my own, shouldn't you have removed Clone and Copy Derives after there work was done, it would have removed unnecessary confusions. Keep making these learnable videos without making them into dull tutorial. Thank you again. ❤
@AndreCarneiro666
@AndreCarneiro666 2 жыл бұрын
So much effort to return a variable!
@codetothemoon
@codetothemoon 2 жыл бұрын
hah! luckily it's a bit of a corner case...
@sergiuoanes4635
@sergiuoanes4635 2 жыл бұрын
this really helped me a lot understanding these concepts. ...huge THANK YOU!!!
@codetothemoon
@codetothemoon 2 жыл бұрын
glad you found it valuable, thanks for watching!
@flippert0
@flippert0 Жыл бұрын
Thanks for demystifying the lifetime concept. What I didn't get up to this video, was that you introduce the lifetime *in the function* as some form of a guarantee about the variables not going out of scope, but it is the *caller* of the function to make sure this guarantee is upheld .
@Omegka
@Omegka 2 жыл бұрын
Great video. Really clear explanation.
@codetothemoon
@codetothemoon 2 жыл бұрын
Thanks, very happy you found it valuable!
@tuanlam999
@tuanlam999 2 жыл бұрын
Very clear explanation. Thank you.
@codetothemoon
@codetothemoon 2 жыл бұрын
Thanks for watching Tuan!
@filipskoras
@filipskoras Жыл бұрын
I believe the issue with Rust lies not in a lack of understanding about how ownership works, but rather in people's struggle to navigate the limitations it presents. It would be wonderful to come across a video showcasing real-life examples of potential problems and effective strategies for mitigating them.
@codetothemoon
@codetothemoon Жыл бұрын
Yeah I think you're probably right about that. Maybe check out this video on interior mutability - kzbin.info/www/bejne/fqjYoYGcbs52gM0 it might have what you're looking for
@NovaAquarius
@NovaAquarius 2 жыл бұрын
Crystal clear explanation. I tried to learn rust a while but everytime I came across borrowing errors I got frustrated and ended up not persuing rust. Maybe after this video I will try again
@codetothemoon
@codetothemoon 2 жыл бұрын
Thanks Nova, glad you found it valuable!
@kennystrawnmusic
@kennystrawnmusic 2 жыл бұрын
One thing you could also do to solve the issue that allows you to do away with the angle brackets in the struct is to define the field as a reference with a lifetime of “‘static” - that’s a reserved lifetime name that says “this lives as long as the program itself.” Only problem there, of course, is that you can leak memory if not careful
@codetothemoon
@codetothemoon 2 жыл бұрын
Good point, I think that is a potential approach too, if your program allows for it
@kennystrawnmusic
@kennystrawnmusic 2 жыл бұрын
@@codetothemoon OS kernel is definitely something that does, so yes.
@kocokan
@kocokan 5 ай бұрын
My golden rule: - borrowed value as fn params - return value
@codetothemoon
@codetothemoon 5 ай бұрын
yep that's definitely a common scenario!
@AbhishekBajpaiHere
@AbhishekBajpaiHere 2 жыл бұрын
Loved it, thanks for the explanation
@codetothemoon
@codetothemoon 2 жыл бұрын
Thanks for watching Abishek!
@bornfree465
@bornfree465 Жыл бұрын
Just the video i was looking for ...awesome!
@codetothemoon
@codetothemoon Жыл бұрын
thanks, glad you got something out of it!
@carrotstickz7290
@carrotstickz7290 2 жыл бұрын
thanks for the clear and concise video!
@codetothemoon
@codetothemoon 2 жыл бұрын
Thanks Carrot! It'd be fair to attribute at least some of that clarity to your abundant levels of vitamin A!
@edu3393
@edu3393 2 жыл бұрын
Valeu!
@codetothemoon
@codetothemoon 2 жыл бұрын
Wow thank you so much, I really appreciate this!!
@АнтонМайоров-ж1л
@АнтонМайоров-ж1л 2 жыл бұрын
Thank you for your videos! Really want to watch video about traits and derive from you.
@codetothemoon
@codetothemoon 2 жыл бұрын
Glad you found them valuable! I'll put traits and derive on the video ideas list!
@AssemblyWizard
@AssemblyWizard 2 жыл бұрын
Great video, I think you should have also explained here about non-lexical lifetimes (at 9:20 you used `bigger` on line 25 to prevent its NLL, but this can confuse many beginners as to why some scenarios don't give an error when you said they should), and also slices (I was very confused about them as a beginner)
@codetothemoon
@codetothemoon 2 жыл бұрын
Great points - I'd love to go into lifetimes in more detail in a future video. Slices too!
@mateusoliveira2776
@mateusoliveira2776 2 жыл бұрын
I would love a bigger video on this with a focus on examples! Great vid
@codetothemoon
@codetothemoon 2 жыл бұрын
Thanks, bigger examples are on the way!
@correabuscar
@correabuscar 2 жыл бұрын
6:45 I'm wondering now if the call does indeed need the explicit &mut still? I imagine yes, but woulda been great to know for sure (yeah I could test it myself I guess)
@DashLiu
@DashLiu 6 ай бұрын
got the concepts,thank you very much!
@codetothemoon
@codetothemoon 6 ай бұрын
nice, thanks for watching!
@schmoris
@schmoris Жыл бұрын
This was incredibly useful, thanks a lot!
@codetothemoon
@codetothemoon Жыл бұрын
nice, really happy you found it valuable!
@maxreuv
@maxreuv 2 жыл бұрын
Very well delivered and easy to follow! Thank you!
@codetothemoon
@codetothemoon 2 жыл бұрын
thanks maxreuv, glad you found it valuable!
@jambangpisang5809
@jambangpisang5809 2 жыл бұрын
Thank you for the indexed video!
@codetothemoon
@codetothemoon 2 жыл бұрын
Thanks for watching Jambang!
@kevvvinnn
@kevvvinnn 2 жыл бұрын
Is it best practice to use lifetimes on all struct fields? When would one be okay with a field going out of scope before the struct?
@codetothemoon
@codetothemoon 2 жыл бұрын
Only for fields that are read-only references. I'm still not entirely sure why the compiler can't infer the lifetimes of read only reference fields.
@Gemasted
@Gemasted 2 жыл бұрын
Good stuff :), though your explanation of copy was a bit lacking, it's not just that it's implicit it's also that it requires the memory copy-able one to one, this works great when you have a struct full of primitives since copying the memory is fast, however if your struct contained pointers to heap memory such as box or vec then you wouldn't be able to implement copy since just copying the memory would create a cloned object with the same references. Furthermore copying the struct especially when it only has 1 primitive field is a zero cost abstraction and so it would be no different than giving the print struct a reference.
@codetothemoon
@codetothemoon 2 жыл бұрын
Jannick - you're so right! Thanks for pointing this out. I'm going to start an errata post and mention this and the incorrect statement I made about segfaults in C++...
@cly15
@cly15 Ай бұрын
great lecture!! thank you so much
@codetothemoon
@codetothemoon Ай бұрын
thanks, glad you found it valuable!
@sharperguy
@sharperguy Жыл бұрын
The thing about Rust is it is an incredibly deep and powerful language. And for good reason. But that means that it will take longer to learn than many of the most popular languages out there today. So the productivity curve for a new developer will start off relatively slow, but as they gain experience eventually they will end up being able to build things much more quickly and much higher quality than in other languages.
@codetothemoon
@codetothemoon Жыл бұрын
agree! 💯
@keludgraphica80
@keludgraphica80 Жыл бұрын
that's pretty much the same with other languages, like C++ for instances. You start slow that when you gain understanding and experiences your productivity will rose
@ziiirozone
@ziiirozone 2 жыл бұрын
Very good video for beginners, i would have liked to see it myself a bit before, when i was struggling to understand and use borrowing and references, but it did make me understand lifetimes well, very good job !
@codetothemoon
@codetothemoon 2 жыл бұрын
thanks ziii, glad you found it valuable!
@gabkov
@gabkov 2 жыл бұрын
at 10:56 when you remove the scope, wouldn't it have work without the lifetime specifier as well? since everything is in the same scope
@codetothemoon
@codetothemoon 2 жыл бұрын
great question - actually the explicit lifetimes are necessary to get the `biggest` function to compile, irrespective of how it is called. So if we removed the explicit lifetimes after removing that scope, we'd get compilation errors referring to the function itself on line 10
@Kiwifruit00
@Kiwifruit00 2 жыл бұрын
Thank you for this! I really love your content and would like to see more.
@codetothemoon
@codetothemoon 2 жыл бұрын
Thanks for watching, more is on the way!
@JanVotava
@JanVotava 2 жыл бұрын
Great video. You just made me to try clean my laptop screen with that grey line on your camera background 😅.
@codetothemoon
@codetothemoon 2 жыл бұрын
Thanks and hah! Green screen keying is tricky to get exactly right sometimes.... 🙃
@llothar68
@llothar68 2 жыл бұрын
As a C++ Developer i don't understand. Can you please make a video where you explain all this from the compiler implementation side. If you know how a computer works. Implementation Descriptions are usually the best. For me it all seems overkill that gets into the way 99% of the time while saving my arse only in 1% of the time.
@codetothemoon
@codetothemoon 2 жыл бұрын
The really interesting thing is - I believe borrowing and lifetimes actually don't have any effect on the machine code generated. They only have an impact on whether the program compiles or not - in that sense you might think of them as developer-only abstractions. Re: overkill, Rust definitely isn't for everyone or for all applications.
@hamdysaadpersonal
@hamdysaadpersonal 2 жыл бұрын
Really it's a great video , keep going and we wand bigger projects 💙🔥
@codetothemoon
@codetothemoon 2 жыл бұрын
Thanks Hamdy! Bigger projects are on the way!
@torphedo6286
@torphedo6286 4 ай бұрын
Thanks, this helped a lot. I have a background in C and I found the error messages refreshingly detailed, but they were kind of cryptic, especially for ownership. I'm used to all function arguments being shallow copies by default and that behaviour was confusing. Having just run into some stack-related lifetime issues in C, these extra checks seem like they could be really nice. I'm not a big fan of the syntax but I like these checks in principle.
@TheBrazilRules
@TheBrazilRules Жыл бұрын
This is so easy for people who worked with pointers before. Really says a lot about the state of programming nowadays
@codetothemoon
@codetothemoon Жыл бұрын
glad to hear that it's easy for some - I personally found it a bit challenging to grasp at first despite having substantial C++ experience
@TheBrazilRules
@TheBrazilRules Жыл бұрын
@@codetothemoon Maybe because you don't had anyone to explain it to you through examples?
@ToukiMS
@ToukiMS 2 жыл бұрын
Thank you for such clear explanations !
@codetothemoon
@codetothemoon 2 жыл бұрын
Thanks, happy you found it valuable!
@CharlesJacobs-n8y
@CharlesJacobs-n8y 2 ай бұрын
Thanks for simplifying the difficult parts!
@codetothemoon
@codetothemoon 2 ай бұрын
glad you found it valuable!
@sbx1720
@sbx1720 2 жыл бұрын
Great quick and concise tutorial. Thank you. At 5:06 you could have deleted the #derive Clone Copy to show that passing by references doesn't implicitly Copy.
@codetothemoon
@codetothemoon 2 жыл бұрын
Thanks for watching! And good point, I probably should have done that
@sunhsiang6644
@sunhsiang6644 Жыл бұрын
Please tech us how to convert error,you know many other lib have various errors type but don’t want to hand it by using match pattern one by one🙏
@codetothemoon
@codetothemoon Жыл бұрын
Check out my other video, "Rust's most important containers". I think what you're looking for might be in there - it talks about how to map one error type to another, convert an error to an option, and some other stuff that may be relevant.
@robomasticus
@robomasticus 2 жыл бұрын
at 10:20. He adds the syntax to specify lifetimes using . I'm trying to understand what this code says. Is he saying the lifetime of parameter a, b, and the return value are all the same as the lifetime of 'a? I do not claim to understand the borrow checker or lifetimes so disclaimer.
@codetothemoon
@codetothemoon 2 жыл бұрын
That's right! basically we're saying that when we call the function, both arguments passed into the function can't go out of scope before the value that we assign the function's return value does. So the values passed in for both parameters need to live at least as long as whatever the return value is assigned to. Hopefully that helps!
@1Dr490n
@1Dr490n Жыл бұрын
I learned this ownership & borrowing system (as well as most of the things I know about rust (not a lot)) by making a Compiler for a language that also uses that so I wrote some simple Rust code, compiled it to LLVM IR and tried to understand it. Best learning technique👍
@samuelbeausoleil1562
@samuelbeausoleil1562 2 жыл бұрын
Question related to the lifetimes used in your "fn biggest()". What if both structs are very memory heavy and I wish to drop the one not returned by the biggest(). Suppose that it is from a library and thus I can't modify it, how would I proceed to do that?
@codetothemoon
@codetothemoon 2 жыл бұрын
Good question, I think the brute force way to do this I believe would be to do mem::drop
@ArnabGuides
@ArnabGuides 2 жыл бұрын
this is explanation to the moon. Keep the tutorials coming. Thanks :)
@codetothemoon
@codetothemoon 2 жыл бұрын
glad you found it valuable, more are on the way!
@KuldeepYadav-jw7jn
@KuldeepYadav-jw7jn 2 жыл бұрын
Hi, it’s great video explaining these fundamental powerful feature of rust but I am still not sure about the lifetime, in your example when we are talking about the scope, what we are referring here reference of two structs passed to the function or the real value of two structs?
@codetothemoon
@codetothemoon 2 жыл бұрын
we're referring to the two structs passed to the function. `other_struct` goes out of scope because it and is inside a block of curly braces, but biggest returns other_struct and assigns it to a variable that is going to live longer than other_struct - this is why we need the lifetime annotations on the function parameters - so that the compiler can catch and flag things like this as errors.
@KuldeepYadav-jw7jn
@KuldeepYadav-jw7jn 2 жыл бұрын
@@codetothemoon when you say the struct passed to the function, you mean the pointer to those structs, right? But since these pointers are borrowed pointers from calling function, these will be taken out of scope by the called function, if we don't specify lifetime, am I right here? If this ia true then what borrowing means here, does it mean pointer is copied and then passed to another function? Because if it is not, then compiler should know the scope of pointer is larger than the called function and we should not need lifetime.
@alwin5995
@alwin5995 2 жыл бұрын
Been building a project in actix Web with Diesel.....seriously, Diesel is insanely easy to setup and running DB queries on Rust is a charm....
@codetothemoon
@codetothemoon 2 жыл бұрын
Nice! Yeah it seems like Diesel has a bit of a learning curve especially if you haven't dealt with automatic schema migration before. But it seems pretty nice once you have a handle on everything.
@YouTube_Staff
@YouTube_Staff Жыл бұрын
Are those box switches on your keyboard? :D
@codetothemoon
@codetothemoon Жыл бұрын
pretty sure I used the Redragon K552 with blue (clicky) switches for this one
@xxdeadmonkxx
@xxdeadmonkxx Жыл бұрын
Is it a good idea to return the argument from a function so we can acquire ownership back?
@codetothemoon
@codetothemoon Жыл бұрын
I think just passing a mutable reference to the function might be a better - trying to think of a case where you couldn't achieve the same things using that approach, and I haven't been able to come up with one
@phutchins3
@phutchins3 2 жыл бұрын
Very well explained, thank you!
@codetothemoon
@codetothemoon 2 жыл бұрын
Thanks for watching, glad you found it valuable!
@pvc988
@pvc988 2 жыл бұрын
The cleanest explanation I've seen on YT.
@codetothemoon
@codetothemoon 2 жыл бұрын
Thanks pvc!
@xtiooplus
@xtiooplus 11 ай бұрын
I've finally understood it! THANKS
@codetothemoon
@codetothemoon 10 ай бұрын
nice! really happy you got something out of it!
@neunmalelf
@neunmalelf Жыл бұрын
There are at a lot of other memory save languages, and they are also fast. However, their builders are more focused on optimizing compiler speed and less on internal quarrels.
@israrkarimzai
@israrkarimzai Жыл бұрын
Thank you. Amazingly explained.
@aspergale9836
@aspergale9836 2 жыл бұрын
9:34 - This looks like VS Code. How are you getting these argument name hints to the left of the passed variables/expressions (e.g. on lines 23 and 25)??
@Liam_The_Great
@Liam_The_Great 2 жыл бұрын
that's just the rust analyzer extension
@upriser77
@upriser77 2 жыл бұрын
Thank you so much! I've finally understood the lifetime.
@codetothemoon
@codetothemoon 2 жыл бұрын
Thanks for watching Chan, glad you found it valuable!
@yashjain5938
@yashjain5938 5 ай бұрын
How to deal with the case when you keep the scope of values different as even after providing the lifetime you were getting the error when the values were in different scope, so how to handle this error? Like keeping the values in different scopes then using lifetime and then won't get any error
@mrstringrider9568
@mrstringrider9568 Жыл бұрын
Can you please prepare a video on Rust logging vs Tracing in case of server side telemetry
Rust's Alien Data Types 👽 Box, Rc, Arc
11:54
Code to the Moon
Рет қаралды 155 М.
All Rust features explained
21:30
Let's Get Rusty
Рет қаралды 331 М.
СИНИЙ ИНЕЙ УЖЕ ВЫШЕЛ!❄️
01:01
DO$HIK
Рет қаралды 3,3 МЛН
Что-что Мурсдей говорит? 💭 #симбочка #симба #мурсдей
00:19
Rust Powered Polymorphism ⚡️ With Traits
9:55
Code to the Moon
Рет қаралды 107 М.
but what is 'a lifetime?
12:20
leddoo
Рет қаралды 83 М.
Rust's Most Important Containers 📦 10 Useful Patterns
17:11
Code to the Moon
Рет қаралды 131 М.
rust runs on EVERYTHING (no operating system, just Rust)
18:10
Low Level
Рет қаралды 365 М.
Rust is not a faster horse
11:37
No Boilerplate
Рет қаралды 333 М.
you need to build a RUST desktop app!!
27:21
Travis Media
Рет қаралды 338 М.
Rust Interior Mutability - Sneaking By The Borrow Checker
16:02
Code to the Moon
Рет қаралды 75 М.
Use Arc Instead of Vec
15:21
Logan Smith
Рет қаралды 157 М.
Rust makes you feel like a GENIUS
10:48
No Boilerplate
Рет қаралды 413 М.
Rust Lifetimes Finally Explained!
19:10
Let's Get Rusty
Рет қаралды 167 М.
СИНИЙ ИНЕЙ УЖЕ ВЫШЕЛ!❄️
01:01
DO$HIK
Рет қаралды 3,3 МЛН