Lifetimes - Rust

แชร์
ฝัง
  • เผยแพร่เมื่อ 23 พ.ย. 2024

ความคิดเห็น • 48

  • @danlogs
    @danlogs  4 ปีที่แล้ว +11

    In the last section of the video (17:31 "Let's put it all together"), you might have noticed that when we encountered the error (@ 21:30), to fix it, I changed not only the function signature but also the struct signature - which, of course, didn't really impact the output of the code at all as we 𝙙𝙞𝙙𝙣'𝙩 𝙪𝙨𝙚 𝙩𝙝𝙚 𝙨𝙩𝙧𝙪𝙘𝙩. While this doesn't impact the general conclusion, I just wanted to display that my initial intention with the code was this:
    𝚌𝚘𝚗𝚜𝚝 𝙳𝙰𝙽 : 𝚒𝟹𝟸 = 𝟸𝟺𝟷;
    𝚜𝚝𝚛𝚞𝚌𝚝 𝙲𝚘𝚘𝚛𝚍𝚒𝚗𝚊𝚝𝚎{
    𝚡: &'𝚊 𝚒𝟹𝟸,
    𝚢: &'𝚋 𝚒𝟹𝟸
    }
    𝚏𝚗 𝚡_𝚘𝚛_𝚌𝚘𝚗𝚜𝚝(𝚡: &'𝚊 𝚒𝟹𝟸, 𝚢: &'𝚋 𝚒𝟹𝟸) -> &'𝚊 𝚒𝟹𝟸{
    𝚒𝚏 𝚡 > 𝚢{
    𝚡
    }
    𝚎𝚕𝚜𝚎{
    &𝙳𝙰𝙽
    }
    }
    𝚏𝚗 𝚖𝚊𝚒𝚗() {
    𝚕𝚎𝚝 𝚡 = 𝟻;
    𝚕𝚎𝚝 𝚛;
    {
    𝚕𝚎𝚝 𝚢 = 𝟷𝟶;
    𝚕𝚎𝚝 𝚌 = 𝙲𝚘𝚘𝚛𝚍𝚒𝚗𝚊𝚝𝚎{𝚡: &𝚡, 𝚢: &𝚢}; // ★
    𝚛 = 𝚡_𝚘𝚛_𝚌𝚘𝚗𝚜𝚝(𝚌.𝚡, 𝚌.𝚢); // ★
    }
    𝚙𝚛𝚒𝚗𝚝𝚕𝚗!("{}", 𝚛);
    }
    Fundamentally, I forgot to create an instance of the struct and use its' parameters as the parameters for the function (where the ★ is commented). In any case, the logic is still the same ~ that is, the code would still not work with just one lifetime specifier for the same reasons!
    Special thanks to Reddit user 𝗽𝗿𝗼𝗻𝘃𝗶𝘀 for pointing this out! I hope this helps anyone else that was confused by it ~ if you have any other questions regarding this, don't hesitate to leave questions below ッ

    • @Dorumin
      @Dorumin 4 ปีที่แล้ว +1

      Wonderful to have this correction, figured ignoring the dead_code warning wasn't particularly intended, thanks for the clarification

    • @anengineerandacat
      @anengineerandacat 3 ปีที่แล้ว

      Good thing I saw this lol, thanks for the clarification

  • @GoblinGreen911
    @GoblinGreen911 3 ปีที่แล้ว

    thank you very much for taking the time,
    other people sell courses and they don't even explain as good as you explain.

  • @mohaniya15
    @mohaniya15 ปีที่แล้ว

    There are so many videos that i have watched that dont explain lifetimes as an "agreement with the compiler" or show what benefits it provides to the user, this video made me understand all of it completely!
    Thanks a lot, subscribed!

    • @danlogs
      @danlogs  ปีที่แล้ว

      This kind of comment is what keeps me going - Thank you!

  • @ManasTunga
    @ManasTunga 3 ปีที่แล้ว +1

    A Well explained tutorial on life time in rust !!! thanks for this video.

  • @silvervaldvee7972
    @silvervaldvee7972 4 ปีที่แล้ว

    The shortest, most concise and clear tutorial so far. The video quality is very good, same for audio and I like the way you speak. Thanks, you are helping me out!

  • @mani98
    @mani98 4 ปีที่แล้ว +3

    Best explaination of lifetimes I’ve had so far, subscribed.

    • @mani98
      @mani98 3 ปีที่แล้ว

      Anyone reading this thread please be aware that flixzone currently is most likely a scam site that tries to attract people using youtube bots

  • @TBaguette
    @TBaguette 4 ปีที่แล้ว +4

    Thanks to your video, I finally understand how to deal with lifetimes and I can't thank you enough for this. Great content, keep it up!

  • @venuyanamandra5318
    @venuyanamandra5318 4 ปีที่แล้ว

    The best explanation. Thank you for sharing your knowledge.

  • @AngelOnFira
    @AngelOnFira 4 ปีที่แล้ว

    For some reason this structured walkthrough and examples really did well for me. Thanks for the video!

  • @ehsannoureddinmoosa7881
    @ehsannoureddinmoosa7881 4 ปีที่แล้ว +1

    The best one explanation of lifetime. Im going to watch other videos now. Keep it up

  • @inspirated
    @inspirated 4 ปีที่แล้ว

    Awesome, awesome. You need to make more Rust videos. Subscribed.

  • @ifergus3790
    @ifergus3790 4 ปีที่แล้ว

    Excellent explanation!

  • @randomuser2579
    @randomuser2579 4 ปีที่แล้ว +6

    Really liked the lifetime Elision thing and the explaination of why we sometimes dont have to specify lifetimes for things. Now I have to subscribe. :)

    • @danlogs
      @danlogs  4 ปีที่แล้ว +1

      Thank you so much! ~ I’m really glad to know it helped you!

  • @MostafaSaad
    @MostafaSaad 4 ปีที่แล้ว

    I'm not even into rust programming but this video is very helpful and the small bits structure helps digesting it at my own pace

  • @goriunovd
    @goriunovd 4 ปีที่แล้ว +2

    Love your Rust videos, I am currently going through the whole Play List, hope to see soon more chapters covered, thank you, please continue your great work !! :)

    • @danlogs
      @danlogs  4 ปีที่แล้ว +1

      Thank you so much! My current plan is to finish the entire book ~ so stay tuned ^^

  • @ze2like
    @ze2like 4 ปีที่แล้ว +8

    Thank you for this quality content :) You're a great teacher !

  • @GamerSenior
    @GamerSenior 4 ปีที่แล้ว

    Thank you, Dan. I was struggling to wrap my head around the concept of lifetimes and you video was a huge help.
    Got yourself a new subscripter. Keep on the amazing work you got here ;)

  • @liveonphoenix5045
    @liveonphoenix5045 ปีที่แล้ว

    @21:30, Hello, could you kindly provide some clarification on the whereabouts of the 'Coordinate' object or instance? I'm curious whether you employed any form of code optimization, such as elision, or if you implicitly utilized it in a way we don't get/catch up. Thank you in advance, and your explanation is truly well-done.

    • @danlogs
      @danlogs  11 หลายเดือนก่อน

      Hey - apologies on the delay getting back to you. That was actually a mistake on my part. I meant to make use of it, but, iirc, I just kind of forgot about the struct. I have a (now) pinned comment on this video w/ further details on how I intended to use it.

  • @TheStuzenz
    @TheStuzenz 4 ปีที่แล้ว

    Dan, this was really nice content - I really appreciate all the help you have been giving - thanks!

  • @casa421
    @casa421 4 ปีที่แล้ว

    Congrats on the initiative of doing all theses videos :)

  • @AyoDamilareMichael-g3y
    @AyoDamilareMichael-g3y 7 หลายเดือนก่อน

    Awesome video ❤

  • @Cosmos-g2n
    @Cosmos-g2n 3 ปีที่แล้ว

    great explanations

  • @GregWoodsLancs
    @GregWoodsLancs 4 ปีที่แล้ว

    great video. I've been going through The Book (which really needs more work on lifetimes), and Rust By Example, which fills in many of the blanks. As a noob, it kind of feels like rust's awesome compiler isn't so awesome when it comes to lifetimes. It feels like several really common scenarios need unnecessary lifetime boilerplate. The reasons will probably become clearer to me as I gain more experience. But regardless, you gained another subscriber!

    • @danlogs
      @danlogs  4 ปีที่แล้ว +2

      Rust is still a fairly new programming language and, consequently, so is its' compiler. While I do think it is possible we'll we recongize more programming patterns and improve abstraction as we go on, don't be discouraged to learn now how to get low-level! I guarantee you that the more you expose yourself to it the easier the concept of lifetimes will get ッ

  • @babayaga4329
    @babayaga4329 4 ปีที่แล้ว

    Really nicely explained. ty

  • @SantoLucasST
    @SantoLucasST 4 ปีที่แล้ว +1

    Nice video. The only problem is that the video says the same thing as the book, even the examples.

    • @danlogs
      @danlogs  4 ปีที่แล้ว +1

      Yep, I try to follow the book as closely as I can! I give some of my own examples here and there but, overall, I aim to not deviate too much and provide almost like an audio version of Steve and Carol's book ^^

  • @chon-850
    @chon-850 4 ปีที่แล้ว

    good explanation. thank you!

  • @patatasdelpapa
    @patatasdelpapa 4 ปีที่แล้ว +1

    Can you explain Anonymous lifetimes please?

    • @danlogs
      @danlogs  4 ปีที่แล้ว +3

      Sure thing!
      The Anonymous Lifetime ('_), is something that allows you to explicitly mark where a lifetime is elided (by the lifetime elision rules) just in case it is unclear. For example, if you have the following struct:
      ```
      struct Something Something {
      Something { smt: string }
      }
      ```
      We know that the compiler understands it as:
      ```
      fn whatever Something {
      Something{ smt: string }
      }
      ```
      This means that you are aware that elision is happening here and chose to display it. Sometimes it helps make code less verbose and maybe clearer. Like here:
      ```
      // verbose
      impl {
      // elided
      impl Something

  • @YeWison
    @YeWison 4 ปีที่แล้ว

    Basically, you just convert the official guide into video way (with the same example as well), but honestly, it works very well:) Btw, actually I think you can use `nodemon` to implement the auto `cargo run`, it works very very handy in my project:)

    • @danlogs
      @danlogs  4 ปีที่แล้ว +2

      Pretty much! When I talked to Steve (one of the co-writers of The Book) I suggested the series to be an audio version of his work ~ here and there I add sort of my own spin to things but, mostly, I try to stick to delivering content that has been reviewed several times haha - in any case, I'm glad you liked it!
      I never heard of nodemon for cargo run ~ I thought that was just for node projects but I do I know this one: github.com/passcod/cargo-watch 🤔

  • @JSTpacek
    @JSTpacek 4 ปีที่แล้ว

    is the problem with longest function solvable only with static lifetime (but not for example by declaring two lifetimes)? I tried but compiler complained about different lifetimes in argument and returned reference

    • @danlogs
      @danlogs  4 ปีที่แล้ว

      This is a really nice question!
      The problem with writing two lifetimes in this example is because our output can only have one lifetime parameter ~ meaning, eventually, we have to choose between 'a and 'b (the compiler can't do it for us). Now, I designed the code this way to see the problem arise but, in reality, it is sorta nonsensical and you'd probably just refactor the code to fix it ッ

  • @syedghufranhassan3858
    @syedghufranhassan3858 4 ปีที่แล้ว

    awesome bro. Bro in case if there is any query may I email you ?

    • @danlogs
      @danlogs  4 ปีที่แล้ว +1

      Heyo! ~ Sorry for taking a while to reply, seeing your comment this morning is what gave me the final push to finally create a Discord server ッ
      Sooo... Yeah! The best way to reach me is through here: discord.gg/fSWE49H

    • @syedghufranhassan3858
      @syedghufranhassan3858 4 ปีที่แล้ว

      @@danlogs That's great keep it up. Also please cover trait env_logger in rust.

  • @CypElf
    @CypElf 4 ปีที่แล้ว

    Great. Thanks