Rust Lifetimes

Ғылым және технология

An introduction to Rust Lifetimes through example.
In addition to this video, Arseniy Kucherenko has made an addendum to this video talking about static lifetimes. Very informative! I recommend you check it out.
• Additional examples to...
This Rust programming language tutorial series is aimed at easing your training step by step. Rust is a systems level language aimed at speed and safety and can be run cross-platform, including embedded systems and even the browser with WebAssembly (WASM)! I use the VS Code development environment to help you learn the core topics. Please join me on this journey with this fantastic new programming language.
Topics include coding basics, hello world, primitives, cargo, toml, rustup, arrays, borrowing, shadowing, string vs str, tuples, enumerations (enums), println and so much more...all free on youtube!

Пікірлер: 217

  • @mazharansari4156
    @mazharansari41564 жыл бұрын

    This is the best explanation of the lifetime concept that I found on KZread.

  • @dougmilford7814

    @dougmilford7814

    4 жыл бұрын

    Hey, thanks :)

  • @Mephistel

    @Mephistel

    4 жыл бұрын

    Easily! I wish I had this when trying to learn the first time around, this makes things much clearer.

  • @dougmilford7814

    @dougmilford7814

    4 жыл бұрын

    Yaaayyyyy!!! I'm glad people are getting so much benefit from this video!

  • @karuma1959

    @karuma1959

    4 жыл бұрын

    Absolutely! Thanks Doug, for putting so much work into your Rust tutorials. I've seen tutorials from other people before but this is by far the best! I will definetly recommend it !!!

  • @dougmilford7814

    @dougmilford7814

    4 жыл бұрын

    @@karuma1959 Awesome! Thanks for helping my channel gain traction. I appreciate it :)

  • @ajinkyax
    @ajinkyax4 жыл бұрын

    hands-down the best RUST videos for beginners on youtube.

  • @joyboy4208
    @joyboy420811 ай бұрын

    Even after 3 years, this is still the best explanation there is to rust lifetimes, any chance we'll be seeing a smart pointer, declarative and procedural macros explanation?

  • @bhanuprasad72
    @bhanuprasad722 жыл бұрын

    The best explanation on Lifetimes. I referred a Oreilly book, a nostarch book and spent a entire day. Could not comprehend. This video did it.

  • @rallokkcaz
    @rallokkcaz3 жыл бұрын

    Only a few minutes in and it's already more clear than most guides on the net. Thanks!

  • @phamucmanh1750
    @phamucmanh1750 Жыл бұрын

    I love the way you explain the details of Rust, satisfy me a lot

  • @kid_kulafu_1727
    @kid_kulafu_17272 жыл бұрын

    I read the rust book over and over and didn't understand it until I watched this video. Thank you sir!

  • @eloylp6012
    @eloylp6012 Жыл бұрын

    2 years ago. Still helping people. Thanks a lot !

  • @SaHaRaSquad
    @SaHaRaSquad4 жыл бұрын

    Fantastic explanation, especially by showing the "unnecessary" parts that the compiler usually does automatically. It helped me fix my TryFrom implementation within a minute, and it's much clearer to me now.

  • @dougmilford7814

    @dougmilford7814

    4 жыл бұрын

    Whoo hoo!!! So glad the video helped :)

  • @glennwaters1874
    @glennwaters18743 жыл бұрын

    I have to agree with many of the comments already here... best explanation I found of lifetimes. I think that possibly, maybe, could be that I might understand lifetimes now ;) Thanks Doug!

  • @VladimirDjokic
    @VladimirDjokic5 ай бұрын

    Rustaceans don't look any further, this is the best explanation there is to rust lifetimes!!!

  • @purplerobber
    @purplerobber4 жыл бұрын

    this is by far the most comprehensive explanation of rust lifetime I have ever seen.

  • @dougmilford7814

    @dougmilford7814

    4 жыл бұрын

    Ah, thanks!

  • @rohankapur5776
    @rohankapur57763 жыл бұрын

    what a fantastic coverage of lifetimes in rust. I'm here after reading the section in the rust book and watching this cleared up most of the issues I had with what I read in the book. thank you for this!!!

  • @imjasonmiller
    @imjasonmiller4 жыл бұрын

    Doug, your videos are a fantastic resource! Thank you and have a great day!

  • @dougmilford7814

    @dougmilford7814

    4 жыл бұрын

    You're very welcome! Thanks for the support :)

  • @kevinwang6188
    @kevinwang61883 жыл бұрын

    This is absolutely the best explanation of Rust Lifetimes I've found, either on KZread, online Tutorials or books. Thanks for delivering such a wonderful tutorial video!

  • @ehsankhorasani_
    @ehsankhorasani_2 жыл бұрын

    Thank you very much! that was really helpful. This is actually the best explaintion of lifetime in whole internet

  • @phamucmanh1750
    @phamucmanh1750 Жыл бұрын

    Oh, amazing lesson, you saved my day. Thank you !

  • @sherajussalehin868
    @sherajussalehin8684 жыл бұрын

    Dough, you’re doing a fantastic job with these videos. I usually prefer written materials but when the book doesn’t explain something clearly, your video is usually the answer.

  • @dougmilford7814

    @dougmilford7814

    4 жыл бұрын

    Ah, thanks!

  • @DSaad
    @DSaad8 ай бұрын

    This is the most comprehensive and clear explanation of the concept of lifetime that I have come across on KZread. Thanks a lot!

  • @kvngve8879
    @kvngve88794 жыл бұрын

    I learned a lot from this even after reading the rust book. Thank you!

  • @abhisheksinha9027
    @abhisheksinha90272 жыл бұрын

    I would have quit Rust without this channel. Thank you Doug!!

  • @gouravkhator
    @gouravkhator Жыл бұрын

    Since 3 days I was researching about lifetimes on the articles, and then I saw your video.. It is the only best explained resource that I could find on the internet.. A simple thanks would not be enough to express the time and effort you saved me, learning this concept. And it is such a beautifully explained concept, that I hope that you don't delete this video, or make it private. I would be bookmarking this video on my notes app.

  • @ManasTunga
    @ManasTunga3 жыл бұрын

    These videos of Rust by Doug Milford need to be in the official documentation of rust

  • @yurikolovsky
    @yurikolovsky2 жыл бұрын

    The best explanation I've found on youtube and outside of youtube. Lifetimes never clicked until now, thank you so much!

  • @annismonadjem6901
    @annismonadjem69014 жыл бұрын

    Awesome! Doug's videos are of high quality and condensed with valuable content. Doug is a fantastic educator with unusual abilities to keep you up and focused. This video comes right on time and happens to be very helpful to me in `digesting` the notorious Rust Book. Thanks Doug.

  • @dougmilford7814

    @dougmilford7814

    4 жыл бұрын

    Yep, yep... my pleasure :)

  • @angelbythewings
    @angelbythewings Жыл бұрын

    mithradates channel sent me here, and I am happy for it. This was beautifully explained

  • @thesuperyou2829
    @thesuperyou28292 жыл бұрын

    Well Mr Doug Milford from Lamda valley .. you are one hell of a teacher..

  • @darrenchan9951
    @darrenchan99512 жыл бұрын

    Thanks for clarifying on lifetimes, Doug. Hope you’re doing well since I haven’t seen any recent videos.

  • @samighasemi3333
    @samighasemi33332 жыл бұрын

    I've read the chapter 10 of the Rust Programming Language book, but I found the best explanation here. Thank you so much!

  • @abhimanyu003
    @abhimanyu0032 жыл бұрын

    This video is pure gold, thanks so much.

  • @GoblinGreen911
    @GoblinGreen9113 жыл бұрын

    thank you for blessing us with your divine intellect.

  • @michalbotor
    @michalbotor Жыл бұрын

    this is absolutely brilliant! i had no idea that every reference has a lifetime, that it is implicitly inserted, and that it is different for every reference. knowing this makes rust lifetimes a much easier topic to understand and handle. it is also quite logical. thanks a lot!

  • @meekmtck5917
    @meekmtck59173 жыл бұрын

    Agreed, best lifetime tutorial on the tubes

  • @theblacktiger59
    @theblacktiger59 Жыл бұрын

    The whole serie on Rust is greatly educational ! Very clear explanation, straight to the point, best tutorials on Rust I have found. Thank you for the great content !

  • @mau5mat
    @mau5mat3 ай бұрын

    Incredible resource for this topic here, thank you for your time!

  • @bagwaa9948
    @bagwaa9948 Жыл бұрын

    Literally the best video or explanation I've seen on lifetimes, including all the usual rust books I've read, thank-you!

  • @kevinle4640
    @kevinle46404 жыл бұрын

    Awesome. Your video tutorials are just awesome. Looking forward to and hoping your next topic is "Rust closures/lambdas". Don't forget to inject humors in your videos. They're great.

  • @dougmilford7814

    @dougmilford7814

    4 жыл бұрын

    Thanks! Currently working on Macros videos, but I'll move closures/lambdas up the list.

  • @techzoneplus
    @techzoneplus Жыл бұрын

    Excellent explanation, no need to check the documentation again for my lifetime 😀

  • @hojjat5000
    @hojjat50004 жыл бұрын

    This was great. I tried a few videos, a few examples, and tried to read the Rust book. But this video was so clear, had many examples (much appreciated), not complicated, and to the point. I was able to follow along, write the codes myself, and take notes and not get tired, bored, or lost. Great job. Will come back for more.

  • @akankshasaini9980
    @akankshasaini99804 жыл бұрын

    Exactly what I needed. So many examples, fast enough so I don't get bored.

  • @dougmilford7814

    @dougmilford7814

    4 жыл бұрын

    Glad you like it :)

  • @MasterHigure
    @MasterHigure4 жыл бұрын

    Lifetimes are an integral part of any reference type, although it's often hidden. Any reference can easily be converted (downcast?) to a reference of shorter lifetime. That's why a single generic lifetime, like fn testtwo { one } else { two } } fn main() { let a = "hello"; { let b = "world"; { let c = test(a, b); println!("{}", c); } \\ c dies here } \\ b dies here } \\ a dies here works, even though there are three distinct lifetimes for the three variables. As a and b are passed into the function, a is downcast to a reference with the lifetime of b as it is bound to the variable one. This makes the inputs have the same lifetime, as required. The return therefore has the same lifetime: the lifetime of b. When the returned reference is bound to c, it gets downcast to have the lifetime of c. This isn't to disagree with anything you said. But I don't think it was mentioned explicitly, and coming to this realization is what made lifetimes in function signatures click for me. I do wish this video had existed two years ago, though. Would've helped me out a lot.

  • @dougmilford7814

    @dougmilford7814

    4 жыл бұрын

    You make a good analogy and it may be helpful to others struggling to understand the concept. Thanks!

  • @fastexpand

    @fastexpand

    Жыл бұрын

    Thanks for this!

  • @johnflux1
    @johnflux13 жыл бұрын

    Really good, thank you. I would love a usecase where you'd WANT to have different lifetimes though.

  • @sachin__ak

    @sachin__ak

    2 жыл бұрын

    did you understand the part where he passed a const variable and a normal variable to a function with same lifetime 'a? const was outside the main function and has lifetime of the program and normal variable inside the function has a lifetime of main fn, how can that be equal? is it because main is the last function to exit the stack and the program ends so rust considers the variable declared inside main fn to be the same lifetime of 'static?

  • @deckarep
    @deckarep4 жыл бұрын

    Great talk that helps to drive the intuitive understanding on Rust’s Lifetimes!

  • @davidcodes009
    @davidcodes0094 жыл бұрын

    Thanks for the insightful explanations! I have been going through all of your Rust videos and your explanations are super clear and to the point.

  • @dougmilford7814

    @dougmilford7814

    4 жыл бұрын

    Hey, thanks! I'm glad you like them :)

  • @trevorcoleman9125
    @trevorcoleman91252 жыл бұрын

    Terrific videos -- clear, well constructed, and weirdly entertaining. Thanks for these!

  • @brachypelma24
    @brachypelma24 Жыл бұрын

    Thank you so much for your excellent Rust content, Doug. Your videos are the best guides to learning Rust I have found.

  • @mateusmercer2280
    @mateusmercer22804 жыл бұрын

    Thanks so much! Your explanation solved a lot of issues I've had with this topic.

  • @kerze003
    @kerze0033 жыл бұрын

    Amazing explanation. Thanks Doug!

  • @ianbrierley3233
    @ianbrierley32334 жыл бұрын

    Thanks for the time and effort Doug.

  • @HowdyhoNet
    @HowdyhoNet Жыл бұрын

    1:33 - Howdy, partner!)

  • @krisspujats4058
    @krisspujats40583 жыл бұрын

    These videos are seriously flattening the learning curve... Amazing... Thank you...

  • @pau1976
    @pau19763 жыл бұрын

    Thank you so much for this! Very clear, and detailed, even good humor.

  • @KiffinGish
    @KiffinGish4 жыл бұрын

    Watched this video a couple times and still a bit confused, but a hundred times less confused about lifetimes than I used to be.

  • @dougmilford7814

    @dougmilford7814

    4 жыл бұрын

    Yeah, it's a very tricky topic. Except for creating Macros, I would say it's the toughest topic to wrap your head around in Rust. Hopefully you learned enough to at least be functional. I think a lot of learning it is running into situations and thinking about why you're getting a compile error. Let me know if you run into any specific situations that are giving you trouble and perhaps I can speak directly to that. Sometimes a specific example is what's needed to help clear the fog.

  • @bennythetiger6052
    @bennythetiger60529 ай бұрын

    WOW, I finally undedrstand frickin lifetimes

  • @RodPeg
    @RodPeg4 жыл бұрын

    Very thanks for your videos, your teaching skills are amazing. The best Rust learning content I found.

  • @danvilela
    @danvilela3 жыл бұрын

    I am studying Rust and don’t even know why. But this is very interesting (and weird!)

  • @ClearerThanMud
    @ClearerThanMud2 жыл бұрын

    Great job. I'm going to share this with a new hire.

  • @adrian-colbert
    @adrian-colbert2 жыл бұрын

    Thank you! This really helped brush up on my skills!

  • @mageprometheus
    @mageprometheus4 жыл бұрын

    Hi Doug. I've watched all your videos and love the clarity of concept you produce. A natural teacher. Nameste.

  • @dougmilford7814

    @dougmilford7814

    4 жыл бұрын

    Thanks, Mage! I'm really glad you liked them :)

  • @nurmohammed9642
    @nurmohammed96423 жыл бұрын

    I watched every second of all your rust tutorial series. I would like if you make a video about . closure ( inline function ). Great Work!!!

  • @horaciomlhh
    @horaciomlhh4 жыл бұрын

    really concise and practical explanation! thanks a lot!

  • @dougmilford7814

    @dougmilford7814

    4 жыл бұрын

    Glad it was helpful!

  • @jeffg4686
    @jeffg46863 жыл бұрын

    This was good. Lifetimes are a pain at first, for certain. Thanks for the share.

  • @shivangrathore
    @shivangrathore3 жыл бұрын

    Very nicely explained!

  • @shrestha0144
    @shrestha0144 Жыл бұрын

    What a beautiful explanation

  • @njzz1801
    @njzz180110 ай бұрын

    most helpful video about lifetime

  • @fbytgeek
    @fbytgeek3 жыл бұрын

    these series are too good. I would love to watch a video to learn more about you, how you got involved with Rust, how you learned it, etc. This is some quality content. So thanks!! Question: Do you have a plan to do any videos on Actix web?

  • @simonstrandgaard5503
    @simonstrandgaard55033 жыл бұрын

    Great walkthrough.

  • @alkymistendenmark
    @alkymistendenmark3 жыл бұрын

    Hey, great videos and nice job on the vocal recordings! You forgot to put a link to the memory management video in the description :) or maybe you didn’t think about it, but it would be nice!

  • @edfloreshz
    @edfloreshz4 жыл бұрын

    Doug, you've accomplished what The Rust Book failed to! Thanks so much for the explanation, subscribed!

  • @dougmilford7814

    @dougmilford7814

    4 жыл бұрын

    Hey, great! I'm glad you liked it so much :)

  • @haz4205
    @haz4205 Жыл бұрын

    This was an amazing video, thank you so much

  • @DwarkeshPatel
    @DwarkeshPatel3 жыл бұрын

    thid was super helpful. thank you!

  • @chrisnocker3437
    @chrisnocker34374 жыл бұрын

    Thanks for the great explanation Doug!

  • @dougmilford7814

    @dougmilford7814

    4 жыл бұрын

    Yup, my pleasure!

  • @gustavoaveiro624
    @gustavoaveiro6242 жыл бұрын

    The best explanation!!

  • @Njinx_
    @Njinx_4 жыл бұрын

    You are a god amongst gods. Thank you for this excellent explanation.

  • @yfchan7753
    @yfchan77533 жыл бұрын

    You are the best teacher

  • @Davidicus000
    @Davidicus0004 ай бұрын

    Very nice! Thank you.

  • @darrennew8211
    @darrennew82114 жыл бұрын

    Very nice and clear lecture. At 15:20, I think it would be more appropriate to say that the lifetimes of the parameters passed in are known to outlast the function, because the caller (or someone up the call stack) has the ownership. I.e., it's not because you're not returning a reference, but because you can't deallocate the memory the input reference refers to from inside the function. You necessarily have to return from the function (and drop the reference) before the caller will have a chance to deallocate what the reference was pointing to. If you passed in both the reference and ownership, you could do that, but you wouldn't be able to because of the only-one-mutable rule.

  • @dougmilford7814

    @dougmilford7814

    4 жыл бұрын

    Hey, Darren, thanks! I appreciate it :) The Lifetime of reference parameters passed in for sure outlast the function. So I agree with you on that. If you never return the reference parameter via the output, though, I'm not sure you'll ever get a lifetime issue. Returning the reference is what causes the compile error because of the potential issue of using the resultant reference after memory gets cleaned up (lifetimes are always there in the background... it's just not necessary to make the developer handle them in many situations). I'm not 100% sure what you mean about passing in both the reference and ownership. But maybe I'm just not understanding. If I somehow misinterpreted your question/comment, let me know. I think you're trying to make a good point. I'm just not sure if I fully understand. Maybe if you gave an example that would help me understand? And, if I misspeak at some point, I appreciate when people catch me and correct it in the comments. I do my best to be thorough and correct, but now and then everyone has a slip of the tongue. So thank you for helping clarify if there's confusion :) Doug

  • @nikhilsharad9553
    @nikhilsharad95538 ай бұрын

    Hey Doug, this is really helpful. Could you please make a video explaining the Smart Pointers in Rust?

  • @valinkdevr5520
    @valinkdevr55204 жыл бұрын

    I love how you can write rust without even having to compile and check if everything works

  • @ajinkyax

    @ajinkyax

    4 жыл бұрын

    Thats vscode + Rust RLS -> using Rust Analyzer plugin

  • @secondaryaccount8155

    @secondaryaccount8155

    4 жыл бұрын

    @@ajinkyax Is rust analyzer available for VIM?

  • @evilmorty-tv1xs

    @evilmorty-tv1xs

    4 жыл бұрын

    @@secondaryaccount8155 yep! coc.vim + coc-rust-analyzer works like a charm !

  • @ninjarogue
    @ninjarogue2 жыл бұрын

    wow you do a great a job at breaking down lifetimes, thanks

  • @zeroows
    @zeroows Жыл бұрын

    Thank you for the great videos

  • @Kserx.p
    @Kserx.p4 жыл бұрын

    Thanks! Great video again.

  • @dougmilford7814

    @dougmilford7814

    4 жыл бұрын

    Hey, I appreciate the support :)

  • @erlangparasu6339
    @erlangparasu63392 жыл бұрын

    great tutorial thank u so much 🙏

  • @Nandu2794
    @Nandu27944 жыл бұрын

    Thanks so much for doing this video!!!

  • @dougmilford7814

    @dougmilford7814

    4 жыл бұрын

    Sure thing! I'm glad you liked it :)

  • @Karthik-yy6up
    @Karthik-yy6up3 жыл бұрын

    Great video, you're very coherent. Thank you so much :-)

  • @priyanshu4016
    @priyanshu4016 Жыл бұрын

    this is owsome explaination , you can think to create the full course on rust on udemy or pluralsight , currently there is no course available which explain with this ammount of detials you shared . thanks for your effort

  • @webbhinton6842
    @webbhinton68423 жыл бұрын

    this has taken me months to understand but i'm finally getting there I think

  • @googiek9351
    @googiek93514 жыл бұрын

    Doug, 'a is great, never knew, Thanks for sharing your insights.

  • @dougmilford7814

    @dougmilford7814

    4 жыл бұрын

    Sounds like you got some good stuff out of the video. I'm glad!

  • @googiek9351

    @googiek9351

    4 жыл бұрын

    @@dougmilford7814 definitely! meticulous thing explained in layman terms. thanks again.

  • @myyouaccounttube1024
    @myyouaccounttube10242 жыл бұрын

    Awesome videos! Many thanks for your time and these truly valuable videos that you share with Rust community. And if there is any possibility to increase the font size of your editor (VScode) in your videos, much to the better.

  • @kirill406
    @kirill4063 жыл бұрын

    great video, thx

  • @sippy_cups
    @sippy_cups4 жыл бұрын

    this man is an absolute sav, tysm!

  • @dougmilford7814

    @dougmilford7814

    4 жыл бұрын

    Looks like you're getting a lot out of the videos. I'm glad!

  • @GottsStr
    @GottsStr4 жыл бұрын

    Thank you, Sir. This was really helpful.

  • @dougmilford7814

    @dougmilford7814

    4 жыл бұрын

    Yep, yep... glad you like it :)

  • @Sina-xw4xp
    @Sina-xw4xp4 жыл бұрын

    I certainly enjoyed this talk. wished i watched it earlier :) but still it was informative. tx

  • @dougmilford7814

    @dougmilford7814

    4 жыл бұрын

    Glad you liked it :)

  • @amoshnin
    @amoshnin3 жыл бұрын

    Thanks a lot!

  • @samirbouakaz4518
    @samirbouakaz45184 жыл бұрын

    Félicitation, les tutoriels sont excellents .. Merci

  • @dougmilford7814

    @dougmilford7814

    4 жыл бұрын

    Merci! J'apprécie le compliment.

  • @JitenPalaparthi
    @JitenPalaparthi11 ай бұрын

    Excellent

  • @kikleine
    @kikleine4 жыл бұрын

    You're a life(time)saver!

  • @dougmilford7814

    @dougmilford7814

    4 жыл бұрын

    I'm glad you liked it :)

  • @felipelubedebraganca7678
    @felipelubedebraganca76783 жыл бұрын

    Hi Doug, Your videos are helping me a lot. I'd suggest you to increase the font size. Thank you.

  • @dougmilford7814

    @dougmilford7814

    3 жыл бұрын

    Yeah, the Lifetimes video I totally goofed. I recorded the whole thing and only saw the issue right before I was about to publish. You're not the first to notice on this video that the font's too small.

  • @wannadie2003
    @wannadie20032 жыл бұрын

    thank you so much

  • @theanhmai3637
    @theanhmai3637 Жыл бұрын

    It's very clear, thank you. I am having trouble with Macros. Can you have a video to explain it?

Келесі