Rust Lifetimes Finally Explained!
HTML-код
- Опубликовано: 6 июн 2024
- The ultimate Rust lang tutorial. Follow along as we go through the Rust lang book chapter by chapter.
📝 Get your *FREE Rust Cheat Sheet*: www.letsgetrusty.com/cheatsheet
The Rust book: doc.rust-lang.org/stable/book/
0:00 Intro
0:22 The Borrow Checker
3:03 Generical Lifetime Annotations
10:29 Thinking in Terms of Lifetimes
12:25 Lifetime Annotations in Struct Definitions
13:27 Lifetime Elision
15:35 Lifetime Annotations in Method Definitions
17:07 Static Lifetimes
17:27 Putting it All Together
18:33 Outro
#letsgetrusty #rustlang #tutorial - Наука
📝 Get your *FREE Rust cheat sheet* : www.letsgetrusty.com/cheatsheet
I really like how Rust doesn't hide complexity from developers behind trade-offs, but rather offers helpful tools to manage complexity as well as sensible defaults.
This transparancy i like. I dislike magical garbage collectors and other stuff. This is why rust is so great.
There are arguments for both, depending on your application domain. There is never a one size fits all.
biggest draw for me as well
Well put. I'm still wrangling my brain around some of the stuff but slowly it's getting there and with each step you realise how awesome it really is.
I had trouble understanding the lifetime annotations system, until this video. The key information I needed was "It does not change the lifetime, but specifies relationships" explanation.
Your videos and explanation are clear and easy to follow. Can't wait to see more material. Thanks for going through this and doing all the work.
My pleasure!
Came here to say exactly the same thing
Dido
Frustrated beginner Rust programmer: "Just let me write bad code!!"
The Rust compiler: No. Here's some super helpful tips instead.
That's exactly how I feel writing rust, I want to write code that I know it's wrong but I want to only try it and rust forces me to write good code, it's a love hate relationship 😂
Exactly. We should make a meme like:
Me after learning other languages for hours: Wow Let's make a app/game/website.
Me after learning Rust for weeks: Why doesn't it compile? What lifetime annotation should I put here? Should I borrow that thing?
I love Rust is forcing me to do the right thing. For example it forces me to handle the options, use the match. But also with using the match statement it forces me to use all posibilities. No null. I love it doesn't have null. (Well in safe space though) but i like how its the default. The defaults are chosen perfectly.
@@joelimbergamo639 Having had to debug muuuch bad code I really love this behaviour. Dangling pointers can be a real pita.
🤣🤣🔥
As a Java/Node/python programmer and learning Rust I never understood lifetimes in Rust, you`re the first to make this `click` in my brain. Thank you.
I totally understand lifetime by your video! Thank you so much as a Rust beginner, you made me understand Rust Book 200%!
Yeah he complements the book nicely. I find with technical topics that I need a combination of written material, presentations like this and experimentation/practice to get the richest understanding.
Thank you for explaining lifetimes in a clear and easy to understand manner. I just discovered your series and can’t wait to go through the rest of them! Thank you for the time and effort you have put in to produce these.
I was watching this video on the treadmill, and I was enjoying it so much that I actually worked out longer than I was planning without even realizing it until the video was over! Thanks, Bogdan!
Perfect explanation. You just gave me that little extra information that I was missing to finally get it into my head. Thx man! 👏🏻
Thanks for taking the time to produce these videos mate. Learning Rust concepts alone isn’t easy. I appreciate it.
this channel has been hugely helpful for me trying to learn rust, especially since I've never heard of a language doing 'borrowing' before. thank you so much!
Watching this after going through the chapter helps drive the point home. I'm glad I found this.
I have been learning Rust from youtube and Rust by Example, and your videos always do the best job when it comes to tricky concepts such as ownership and lifestime. Thanks a lot!
This series is outstanding tbh, thank you for your hard work. Understanding each of Rust's features makes me love it so much more!
Your explanation is super clear! I know lifetime now! Thank you
Outstanding quality. Thank you so much for your labor!
Hands down, the best explanation of lifetimes I've ever seen
THIS IS BY FAR THE BEST EXPLANATION. Keep it up man
Bogdan, this video is even clear for me as a code illiterate! Thank you for this and PLEASEEEE keep your lectures this minimal! Cheers!
Thank you so much! This video and the others in this playlist really helps me understand Rust better! ❤
your videos are just so incredible man, extremely grateful for these beautifully articulated insights you put out
Thank you for such a comprehensive lifetimes lesson. The learnings from this video will last a lifetime.
Your explanations of these topics are concise, complete, detailed and you move right along with them. Outstanding job, all of your videos make these ideas very clear. I tip my hat to you sir! :)
This channel is a blessing!
Good explanation of a bunch of things I had a really hard time understanding from the Rust book, when I started learning Rust. This could have spared me a lot of pain 😬
Just let me live life dangerously
Rust Compiler: Nope! Not on my watch ⌚ safety first 🎈
Then do C or assembler.
Awesome! These examples are very clear for rust lifetime.
Your videos are amazing! Thank you so much ❤️
Very clear explanation! Thank you!
wow, I saw these 'a-ticks and couldnt find a good/easy explanation for it. You just explained it so easy and perfect. Thanks!!
These videos have been fantastic, thank you
Resolving a longtime mystery in my mind feels so good.
I'd spam the like button if I could.
Lifetimes are clear now! Thank's a lot Bogdan
You know what? This is currently the best video resource to learn Rust. Thank you Bogdan.
this is a nice little tutorial with so much value. Thanks!
Thanks for the video series, it's a great addition to the book as it gives a different perspective to a lot of things. One thing I wonder about lifetimes (and I'm learning, so maybe I did not grasp the concept fully yet): It seems to me that assigning the same lifetime 'a to ALL parameters of a function plus all returned parameters is the most restrictive way possible in terms of lifetimes. If an actual use of a function fulfills these "strictest" rule, why is it actually required to write these lifetimes out when defining it? I think, in the longest-string example, no different lifetime declaration than this most restrictive one is even possible - if you try it, a bug occurs where you try to return the "other" parameter. I understand it's probably a complex problem for the compiler, maybe NP-complete, to find the optimal setting, but in cases where the most restrictive remaining one works, why not defaulting to this? One possible explanation (and maybe this is the reason) is when you write a library that exports functions, so the compiler does not know how it is actually used, so lifetime declarations must be exported as well. But this would not apply for private functions. Another way to make it easier would be to add a mode (maybe compiler flag) to automatically fill up lacking lifetimes with the most restrictive setting still possible? Or what also might be interesting (maybe this exists) were external tools, a bit like lint, or optimally included in an IDE, that could auto-suggest lifetimes?
This is the best youtube video I have ever watched.
You explain this very succinctly, thank you very much! In my mind, lifetimes are almost intuitive once you think about it. I think I just double guess myself because I expect it to be more complex than it is!
Dude great explanation, I finally got the concept!
Awesome! Good explanation of the lifetime concept. I'm looking forward to deal with different lifetimes for variables and convince the compiler that all I'm doing is well planned 🙈 Thank you very much!!
Super, this video is enough to understand lifetime, thanks a lot ❤️❤️
Hi, Bogdan! I would be really cool to see a video about structs where some fields reference others. More precisely, why it is or is not bad and what are the alternatives.
You cannot do this in Rust.
So thorough and interesting! Thanks!
AWESOME EXPLANATION.!! thANKS SO MUCH!
You explain very well some difficult notions.
Clear as day. Thank you!
I rarely comment on videos, but you did a great job with this. Thanks for sharing!
Great video and examples! Thank you!
This is great explanation. Love it. Thanks.
Great video, thanks for the content!
This was amazingly helpful!
Very nicely done. Thank you 🙏
You state at ~13:15 that the lifetime annotation is saying our struct cannot outlive the reference passed into part:first sentence so if we try to use our variable i (agreed) which is an instance of our struct after part:first sentence has gone out of scope we would get an error.
However, would note that the annotation means that an instance of ImportantExcerpt cannot outlive the reference it holds in its part:first_sentence field and in that regard the reference in the ImportantExcerpt instance is valid, the owning variable novel does not go out of scope prior to ImportantExcerpt going out of scope.
I just wanted to address something that a lot of new rust programmers get wrong, and you also did explain it wrongly. At least it's rarely the case someone wants to do this.
I'm going to copy the code you used for the illustration:
struct ImportantExcerpt ImportantExcerpt
Hey Alexander, thank you for the correction and taking the time to write this out.
To clarify, because there are two input lifetimes both &self and announcement:&str are given their own lifetimes.(Rule #1) Because one of the parameters is &self, the return type gets the lifetime of &self. (Rule #3)
Great video, help me understand the lifetime annotation!
Again, another great episode.
So clear. Thanks.
and you may ask yourself, why did I struggle with this?
Thanks for the explanation. I'd found lifetimes confusing up to now, but the way you demonstrated it, it's quite straightforward!
Best lifetime explanation on youtube
GREAT video. thank you so much.
Great video and explanation, thanks
Thanks for the great content
Thank you : )
Thanks again
Thanks a lot
Thank you for explaining it so well 🙏
Wowie. Thanks Bogdan!
Thanks for such a neat explanation. Just a request the audio is little low
Thanks for this video.
Very nice explanination!!!!
very good man, your videos is easy for beginer like me.❤👍
at 16:50 if we return announcement from the function , then according to lifetime ellisions it'll be assigned lifetime of self, but don't you think that it might be wrong in some cases
yes it will be wrong. im sure it will give some "announcement does not live long enough." type of error
Thank you!
Thank you very much.. Explained very nicely..
Can you do a day event?
Thank you very much. Subbed.
Awesome! thanks a lot!
Great video. Only thing I miss here would be showing how different lifetimes interact with each other inside the fn scope to further illustrate the concept.
awesome !!! thank you
Superb! really good explanation, got some small pieces that help me to get my head around this lifetime thingy.
in the fn longest example, m 11:02 it works if we use string slices instead of Strings, my guess is because these implement the copy trait ? hence the values are moved so the reference to result is valid at the end ?
You're a great teacher. Thank you for making this video! Do you know why lifetimes in Rust are rarely given descriptive names? Almost every example I find uses single-letter names like "a".
Thanks that was useful
U r LEGEND man !!
nice. I like the examples
{
Cool man! It's a great explanation.
}
Amazing videos
Good explaination
I loved to know you like The Wheel of Time as well :D
намного понятнее чем в книжках. Спасибо)
Damn, we have a lot of restrictions, but I wish if I get through all these problems I will have a safer, low level, and fast code.
Btw, I recently saw in google news that google is rewriting some part of android with Rust, wish more companies start adopting it.
It's good to catch the wave early :)
Also the new modules in Linux Kernel may be written in Rust and that would be big!
If the block doesn't end until line 230 but the variable is not used after line 100, does rust deallocate the variable in line 101 or until the block ends?
what is the case for the garbage collector?
Superb
Is it possible to make recursive data types like tree nodes using references and lifetimes? Or do you need to use boxes? Boxes are supposed to allocate space on the heap but not all recursive data structures need the heap and should be able to be implemented entirely on the stack.
So when would you use mixed lifetimes?
What extension is that, that allows you see the types?
Smashed the like button. Didn't smash subscribe, already done that before ;)
Hey Bogdan, haw can I have both, rust-analyzer and RLS working at the same time on vscode? I read that rust-analyzer is the way to go but RLS seems very usefull to my. By the way great work with this channel man, keep going!
As far as I know you can’t use both at the same time. Why not just use rust-analyzer? What specific features do you need from RLS?
Let's start out by talking about ding-a-ling references.
In all seriousness, great video.
I wish I could like this video more than once
You explained really well! and after your explanation I was like is there any single case even where i will need 2 lifetime annotations like 'a and 'b to express my logic! because i think i could do my lifetime annotations for any function or struct just by using 'a only!
because if u look deeply for functions it's just annotating all your parameters with 'a which have the possible to be returned from your function and return 'a on the other hand for struct as the struct will become invalid if a single property containing a ref becomes invalid which means we can always pass a single lifetime through the whole struct?
because i have tried 5-10 examples and all are working with a single annotation perfectly as expected! so let me know your opinion regarding this!
I believe this could use an update now that the non-lexical lifetime analyzer has been updated, correct? For example, I do not get an error with rust 1.72.1 on the code sample at 13:00.
Whoever decided to use single quotes in a non-pairwise fashion in the Rust syntax just wants to see the world burn.
what's the name of the background music ? its relaxing
Have a small question about the last example, what is the lifetime of the variable ann which has a generic type T? Why wasn't it explicitly mentioned, like the other input parameters? Would 'ann' also have the same lifetime as x and y, meaning 'a?