Rust Lifetimes

Поделиться
HTML-код
  • Опубликовано: 11 апр 2020
  • 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
    @mazharansari4156 4 года назад +206

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

    • @dougmilford7814
      @dougmilford7814  4 года назад +19

      Hey, thanks :)

    • @Mephistel
      @Mephistel 4 года назад +9

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

    • @dougmilford7814
      @dougmilford7814  4 года назад +16

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

    • @karuma1959
      @karuma1959 4 года назад +13

      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 года назад +7

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

  • @ajinkyax
    @ajinkyax 4 года назад +65

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

  • @joyboy4208
    @joyboy4208 Год назад +8

    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?

  • @phamucmanh1750
    @phamucmanh1750 Год назад +2

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

  • @kid_kulafu_1727
    @kid_kulafu_1727 3 года назад +1

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

  • @bhanuprasad72
    @bhanuprasad72 3 года назад +2

    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.

  • @eloylp6012
    @eloylp6012 Год назад +3

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

  • @rallokkcaz
    @rallokkcaz 3 года назад +6

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

  • @purplerobber
    @purplerobber 4 года назад +12

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

  • @SaHaRaSquad
    @SaHaRaSquad 4 года назад +5

    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.

  • @imjasonmiller
    @imjasonmiller 4 года назад +20

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

    • @dougmilford7814
      @dougmilford7814  4 года назад +4

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

  • @rohankapur5776
    @rohankapur5776 3 года назад +6

    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!!!

  • @kevinwang6188
    @kevinwang6188 3 года назад +25

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

  • @GoblinGreen911
    @GoblinGreen911 3 года назад +3

    thank you for blessing us with your divine intellect.

  • @ManasTunga
    @ManasTunga 3 года назад +3

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

  • @kvngve8879
    @kvngve8879 4 года назад +9

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

  • @VladimirDjokic
    @VladimirDjokic 6 месяцев назад +2

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

  • @DSaad
    @DSaad 9 месяцев назад +1

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

  • @glennwaters1874
    @glennwaters1874 3 года назад +1

    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!

  • @abhisheksinha9027
    @abhisheksinha9027 2 года назад +1

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

  • @samighasemi3333
    @samighasemi3333 2 года назад +1

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

  • @yurikolovsky
    @yurikolovsky 2 года назад +1

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

  • @ehsankhorasani_
    @ehsankhorasani_ 2 года назад +1

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

  • @sherajussalehin868
    @sherajussalehin868 4 года назад +2

    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.

  • @hojjat5000
    @hojjat5000 4 года назад +2

    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.

  • @johnflux1
    @johnflux1 3 года назад +2

    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?

  • @thesuperyou2829
    @thesuperyou2829 2 года назад +1

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

  • @gouravkhator
    @gouravkhator Год назад +7

    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.

  • @trevorcoleman9125
    @trevorcoleman9125 2 года назад +1

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

  • @deckarep
    @deckarep 4 года назад +4

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

  • @phamucmanh1750
    @phamucmanh1750 Год назад +2

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

  • @akankshasaini9980
    @akankshasaini9980 4 года назад +1

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

  • @theblacktiger59
    @theblacktiger59 Год назад +1

    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 !

  • @MasterHigure
    @MasterHigure 4 года назад +8

    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 года назад +5

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

    • @fastexpand
      @fastexpand Год назад

      Thanks for this!

  • @michalbotor
    @michalbotor Год назад +1

    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!

  • @annismonadjem6901
    @annismonadjem6901 4 года назад +8

    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.

  • @brachypelma24
    @brachypelma24 Год назад +1

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

  • @bagwaa9948
    @bagwaa9948 Год назад +1

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

  • @darrenchan9951
    @darrenchan9951 2 года назад +1

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

  • @krisspujats4058
    @krisspujats4058 3 года назад +1

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

  • @mau5mat
    @mau5mat 4 месяца назад +1

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

  • @angelbythewings
    @angelbythewings 2 года назад +1

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

  • @KiffinGish
    @KiffinGish 4 года назад +3

    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 года назад +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.

  • @mateusmercer2280
    @mateusmercer2280 4 года назад +2

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

  • @pau1976
    @pau1976 3 года назад +1

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

  • @abhimanyu003
    @abhimanyu003 2 года назад +1

    This video is pure gold, thanks so much.

  • @davidcodes009
    @davidcodes009 4 года назад +2

    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.

  • @RodPeg
    @RodPeg 4 года назад +1

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

  • @ianbrierley3233
    @ianbrierley3233 4 года назад +1

    Thanks for the time and effort Doug.

  • @meekmtck5917
    @meekmtck5917 3 года назад +1

    Agreed, best lifetime tutorial on the tubes

  • @kerze003
    @kerze003 3 года назад +1

    Amazing explanation. Thanks Doug!

  • @danvilela
    @danvilela 3 года назад +2

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

  • @techzoneplus
    @techzoneplus Год назад +1

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

  • @kevinle4640
    @kevinle4640 4 года назад +3

    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 года назад +2

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

  • @mageprometheus
    @mageprometheus 4 года назад

    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
    @nurmohammed9642 3 года назад +1

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

  • @adrian-colbert
    @adrian-colbert 2 года назад +1

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

  • @shivangrathore
    @shivangrathore 3 года назад +1

    Very nicely explained!

  • @jeffg4686
    @jeffg4686 3 года назад +1

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

  • @gustavoaveiro624
    @gustavoaveiro624 2 года назад +1

    The best explanation!!

  • @haz4205
    @haz4205 2 года назад +1

    This was an amazing video, thank you so much

  • @Njinx_
    @Njinx_ 4 года назад +1

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

  • @DwarkeshPatel
    @DwarkeshPatel 4 года назад +2

    thid was super helpful. thank you!

  • @horaciomlhh
    @horaciomlhh 4 года назад +2

    really concise and practical explanation! thanks a lot!

  • @shrestha0144
    @shrestha0144 2 года назад +1

    What a beautiful explanation

  • @simonstrandgaard5503
    @simonstrandgaard5503 3 года назад +1

    Great walkthrough.

  • @ClearerThanMud
    @ClearerThanMud 2 года назад +1

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

  • @bennythetiger6052
    @bennythetiger6052 10 месяцев назад +1

    WOW, I finally undedrstand frickin lifetimes

  • @chrisnocker3437
    @chrisnocker3437 4 года назад +1

    Thanks for the great explanation Doug!

  • @edfloreshz
    @edfloreshz 4 года назад +1

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

    • @dougmilford7814
      @dougmilford7814  4 года назад +1

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

  • @njzz1801
    @njzz1801 11 месяцев назад +1

    most helpful video about lifetime

  • @Davidicus000
    @Davidicus000 5 месяцев назад +1

    Very nice! Thank you.

  • @ninjarogue
    @ninjarogue 2 года назад

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

  • @alkymistendenmark
    @alkymistendenmark 4 года назад +3

    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!

  • @fbytgeek
    @fbytgeek 3 года назад +2

    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?

  • @Karthik-yy6up
    @Karthik-yy6up 3 года назад

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

  • @erlangparasu6339
    @erlangparasu6339 2 года назад +1

    great tutorial thank u so much 🙏

  • @darrennew8211
    @darrennew8211 4 года назад +1

    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 года назад +3

      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

  • @zeroows
    @zeroows Год назад +1

    Thank you for the great videos

  • @Kserx.p
    @Kserx.p 4 года назад +2

    Thanks! Great video again.

  • @yfchan7753
    @yfchan7753 3 года назад +1

    You are the best teacher

  • @Nandu2794
    @Nandu2794 4 года назад +1

    Thanks so much for doing this video!!!

  • @nikhilsharad9553
    @nikhilsharad9553 9 месяцев назад +1

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

  • @valinkdevr5520
    @valinkdevr5520 4 года назад +2

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

    • @ajinkyax
      @ajinkyax 4 года назад +1

      Thats vscode + Rust RLS -> using Rust Analyzer plugin

    • @secondaryaccount8155
      @secondaryaccount8155 4 года назад +1

      @@ajinkyax Is rust analyzer available for VIM?

    • @evilmorty-tv1xs
      @evilmorty-tv1xs 4 года назад

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

  • @GottsStr
    @GottsStr 4 года назад

    Thank you, Sir. This was really helpful.

  • @kirill406
    @kirill406 3 года назад +1

    great video, thx

  • @sippy_cups
    @sippy_cups 4 года назад +1

    this man is an absolute sav, tysm!

    • @dougmilford7814
      @dougmilford7814  4 года назад +1

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

  • @samirbouakaz4518
    @samirbouakaz4518 4 года назад +1

    Félicitation, les tutoriels sont excellents .. Merci

  • @amoshnin
    @amoshnin 3 года назад +1

    Thanks a lot!

  • @myyouaccounttube1024
    @myyouaccounttube1024 2 года назад +1

    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.

  • @webbhinton6842
    @webbhinton6842 3 года назад +1

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

  • @googiek9351
    @googiek9351 4 года назад +2

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

    • @dougmilford7814
      @dougmilford7814  4 года назад +2

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

    • @googiek9351
      @googiek9351 4 года назад +2

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

  • @JitenPalaparthi
    @JitenPalaparthi Год назад +1

    Excellent

  • @HowdyhoNet
    @HowdyhoNet Год назад +2

    1:33 - Howdy, partner!)

  • @Sina-xw4xp
    @Sina-xw4xp 4 года назад +1

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

  • @kikleine
    @kikleine 4 года назад +2

    You're a life(time)saver!

  • @wannadie2003
    @wannadie2003 2 года назад +1

    thank you so much

  • @herr6239
    @herr6239 2 года назад

    excellent!

  • @theanhmai3637
    @theanhmai3637 Год назад +1

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

  • @priyanshu4016
    @priyanshu4016 Год назад +1

    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