Rust Lifetimes Finally Explained!

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

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

  • @letsgetrusty
    @letsgetrusty  3 ปีที่แล้ว +14

    📝 Get your *FREE Rust cheat sheet* : www.letsgetrusty.com/cheatsheet

  • @ChumX100
    @ChumX100 3 ปีที่แล้ว +277

    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.

    • @HermanWillems
      @HermanWillems 3 ปีที่แล้ว +22

      This transparancy i like. I dislike magical garbage collectors and other stuff. This is why rust is so great.

    • @kevinmcfarlane2752
      @kevinmcfarlane2752 2 ปีที่แล้ว +18

      There are arguments for both, depending on your application domain. There is never a one size fits all.

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

      biggest draw for me as well

    • @alinmgheorghe
      @alinmgheorghe 6 หลายเดือนก่อน

      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.

  • @thingsiplay
    @thingsiplay 3 ปีที่แล้ว +228

    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.

    • @letsgetrusty
      @letsgetrusty  3 ปีที่แล้ว +14

      My pleasure!

    • @piranhi
      @piranhi 2 ปีที่แล้ว +6

      Came here to say exactly the same thing

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

      Dido

    • @TheOracleFile
      @TheOracleFile 4 หลายเดือนก่อน

      @@NathanHedglin Definitely this in combination with the fact that the borrow checker now uses the passed params to compare the lifetime with result

    • @egonzalez4294
      @egonzalez4294 4 หลายเดือนก่อน +1

      Funny how people understand things differently, for me that was meaningless, what made sense to me was like, "the compiler needs to know the lifetime of the output"
      I thought what would it be like to be the borrow checker, and try to figure out the code.
      These annotations just help the checker figure things out.

  • @Hardzinho_yay
    @Hardzinho_yay ปีที่แล้ว +9

    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.

  •  3 ปีที่แล้ว +330

    Frustrated beginner Rust programmer: "Just let me write bad code!!"
    The Rust compiler: No. Here's some super helpful tips instead.

    • @joelimbergamo639
      @joelimbergamo639 3 ปีที่แล้ว +36

      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 😂

    • @garlicat878
      @garlicat878 3 ปีที่แล้ว +24

      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?

    • @HermanWillems
      @HermanWillems 3 ปีที่แล้ว +15

      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.

    • @vorrnth8734
      @vorrnth8734 2 ปีที่แล้ว +6

      @@joelimbergamo639 Having had to debug muuuch bad code I really love this behaviour. Dangling pointers can be a real pita.

    • @conradmbugua9098
      @conradmbugua9098 2 ปีที่แล้ว

      🤣🤣🔥

  • @richasha670
    @richasha670 3 ปีที่แล้ว +64

    I totally understand lifetime by your video! Thank you so much as a Rust beginner, you made me understand Rust Book 200%!

    • @kevinmcfarlane2752
      @kevinmcfarlane2752 2 ปีที่แล้ว +5

      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.

  • @kumatmebrah9044
    @kumatmebrah9044 ปีที่แล้ว +5

    Watching this after going through the chapter helps drive the point home. I'm glad I found this.

  • @kaushikdr
    @kaushikdr หลายเดือนก่อน +1

    I think I already kind of started to realize that it was relationships between the lifetimes, kind of like how a generic doesn't specify a type, but just relationships between types, but you were the first one that made it clear that:
    1. The purpose of a lifetime parameter is to explain the lifetime of the output parameter.
    2. The smallest lifetime of two inputs with the same lifetime is the one that is used to specify the lifetime of the output.
    Thanks!

  • @lesliedouglas1878
    @lesliedouglas1878 หลายเดือนก่อน +2

    Excellent explanation.. been looking for where this concept was simplified with practical examples like this. Nice job

  • @chsblue2
    @chsblue2 3 ปีที่แล้ว +9

    Perfect explanation. You just gave me that little extra information that I was missing to finally get it into my head. Thx man! 👏🏻

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

    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!

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

    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!

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

    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.

  • @GolangDojo
    @GolangDojo 3 ปีที่แล้ว +15

    Just let me live life dangerously

    • @ajesh-mishra
      @ajesh-mishra 3 ปีที่แล้ว +4

      Rust Compiler: Nope! Not on my watch ⌚ safety first 🎈

    • @vorrnth8734
      @vorrnth8734 2 ปีที่แล้ว +3

      Then do C or assembler.

  • @Lexikon00
    @Lexikon00 3 ปีที่แล้ว +18

    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

    • @letsgetrusty
      @letsgetrusty  3 ปีที่แล้ว +10

      Hey Alexander, thank you for the correction and taking the time to write this out.

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

      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)

  • @bertolucci1985
    @bertolucci1985 3 ปีที่แล้ว +16

    Thanks for taking the time to produce these videos mate. Learning Rust concepts alone isn’t easy. I appreciate it.

  • @johnegan2484
    @johnegan2484 2 ปีที่แล้ว +1

    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.

  • @SansDev
    @SansDev 9 หลายเดือนก่อน

    This series is outstanding tbh, thank you for your hard work. Understanding each of Rust's features makes me love it so much more!

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

    Resolving a longtime mystery in my mind feels so good.
    I'd spam the like button if I could.

  • @dmitriidemenev5258
    @dmitriidemenev5258 2 ปีที่แล้ว +6

    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.

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

      You cannot do this in Rust.

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

    Thank you for such a comprehensive lifetimes lesson. The learnings from this video will last a lifetime.

  • @tobi9648
    @tobi9648 10 หลายเดือนก่อน

    wow, I saw these 'a-ticks and couldnt find a good/easy explanation for it. You just explained it so easy and perfect. Thanks!!

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

    That is, the lifetimes only inform the compiler of the lifetimes that these references must have, and it checks if these conditions are fulfilled in the code, and if not, an error is raised.

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

      Thanks for the comment, it was exactly what I was missing.

  • @oliverhunter1847
    @oliverhunter1847 2 ปีที่แล้ว

    Hands down, the best explanation of lifetimes I've ever seen

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

    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!

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

    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 😬

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

    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!

  • @oyunovacom
    @oyunovacom 2 ปีที่แล้ว +1

    Bogdan, this video is even clear for me as a code illiterate! Thank you for this and PLEASEEEE keep your lectures this minimal! Cheers!

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

    This is the best youtube video I have ever watched.

  • @LagDaemonProgramming
    @LagDaemonProgramming 2 ปีที่แล้ว +1

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

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

    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

    • @proloycodes
      @proloycodes 2 ปีที่แล้ว +1

      yes it will be wrong. im sure it will give some "announcement does not live long enough." type of error

  • @sahilverma4077
    @sahilverma4077 3 ปีที่แล้ว +5

    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.

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

      It's good to catch the wave early :)

    • @ajesh-mishra
      @ajesh-mishra 3 ปีที่แล้ว +5

      Also the new modules in Linux Kernel may be written in Rust and that would be big!

  • @klaymen0
    @klaymen0 2 ปีที่แล้ว +6

    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?

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

    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!

  • @gitgudsec
    @gitgudsec 7 หลายเดือนก่อน

    your videos are just so incredible man, extremely grateful for these beautifully articulated insights you put out

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

    THIS IS BY FAR THE BEST EXPLANATION. Keep it up man

  • @2002budokan
    @2002budokan 2 ปีที่แล้ว

    You know what? This is currently the best video resource to learn Rust. Thank you Bogdan.

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

    Your explanation is super clear! I know lifetime now! Thank you

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

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

  • @vijaysinghbisht3016
    @vijaysinghbisht3016 2 ปีที่แล้ว +1

    Why so less comments? This channel deserves far more attention than this... Sadness

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

    Thank you so much! This video and the others in this playlist really helps me understand Rust better! ❤

  • @twentyeightO1
    @twentyeightO1 2 ปีที่แล้ว +1

    This channel is a blessing!

  • @b_mathemagical
    @b_mathemagical 10 หลายเดือนก่อน

    I rarely comment on videos, but you did a great job with this. Thanks for sharing!

  • @建平許
    @建平許 3 ปีที่แล้ว +1

    Awesome! These examples are very clear for rust lifetime.

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

    Lifetimes are clear now! Thank's a lot Bogdan

  • @kch9001
    @kch9001 7 หลายเดือนก่อน

    Best lifetime explanation on youtube

  • @mmmbug
    @mmmbug 9 หลายเดือนก่อน

    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".

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

    Thank you so much, this is so well explained it finally clicked for me :)

  • @oussamasethoum2755
    @oussamasethoum2755 2 ปีที่แล้ว +1

    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?

  • @pynchia4119
    @pynchia4119 2 ปีที่แล้ว +1

    can you please explain the syntax on line 5 @7.30 ? Don't types follow the colon? Are you assigning a value to the arguments of the call to longest? If so, why isn't an equal symbol used? Are you casting their type instead? And what is the need of using .as_str() in the first place?

    • @aidenlilley1319
      @aidenlilley1319 2 ปีที่แล้ว +1

      I don't understand the first part of the question but the point of calling .as_str() is to get the reference, as this video is about reference lifetimes.

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

      Do you mean the "x: string1.as_str()"? The pattern "var: type = value" only applies to creating variables. The things inside longest(…) are not variables, but passing parameters.
      When calling functions you can use named parameters, meaning the parameters are not passed in the order they are defined in but by the names they are defined as. To do so you specify the parameter name, a colon and then the value. "x: string1" means "For the parameter named x use the value string1".
      These grey, smaller text is generated by the IDE/code editor as hints and are not actually there.

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

    So thorough and interesting! Thanks!

  • @stanislavstudzinskyi2347
    @stanislavstudzinskyi2347 2 ปีที่แล้ว

    Outstanding quality. Thank you so much for your labor!

  • @coldcircuit99
    @coldcircuit99 3 ปีที่แล้ว +6

    I don't know why but rust is giving me some feeling of completion when binary is complete and running as expected.

  • @OggerFN
    @OggerFN 2 ปีที่แล้ว

    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.

  • @chlbri
    @chlbri 2 ปีที่แล้ว

    You explain very well some difficult notions.

  • @michaelkastner3611
    @michaelkastner3611 2 ปีที่แล้ว

    Again, another great episode.

  • @NeotenicApe
    @NeotenicApe 3 ปีที่แล้ว +2

    Your videos are amazing! Thank you so much ❤️

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

    Chap 10 is a kick to the nuts.

  • @nullbeyondo
    @nullbeyondo 2 หลายเดือนก่อน

    I mean, I like Rust's modular system, package manager, traits, functional aspects, and lack of dumb OOP; good stuff... but I SWEAR to god!!-I'm always one step away from wrapping my whole project with `unsafe { ... }`. The "lifetime may not live long enough ... '1 must outlive 'a" error was almost the trigger for me today. :D

  • @strawz1988
    @strawz1988 2 ปีที่แล้ว +1

    Thanks for such a neat explanation. Just a request the audio is little low

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

    Very clear explanation! Thank you!

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

    These videos have been fantastic, thank you

  • @kevincarvalhodejesus4473
    @kevincarvalhodejesus4473 4 หลายเดือนก่อน

    Very well explained, thank you!

  • @dulanjala
    @dulanjala 2 ปีที่แล้ว

    this is a nice little tutorial with so much value. Thanks!

  • @PremalUpadhyay
    @PremalUpadhyay 5 หลายเดือนก่อน

    Hi, can you make series on poem framework of rust? Lack of resource availability is major concern and first obstacle to get started with it. Thanks :)

  • @gauravpoudel7288
    @gauravpoudel7288 2 ปีที่แล้ว

    AWESOME EXPLANATION.!! thANKS SO MUCH!

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

    Let's start out by talking about ding-a-ling references.
    In all seriousness, great video.

  • @fhdiaz87
    @fhdiaz87 2 ปีที่แล้ว +1

    It would had been useful to mention that life time could be coerced. That is a reason to use just a' when actually both parameters have different life times.

  • @murugarajuperumalla5508
    @murugarajuperumalla5508 2 ปีที่แล้ว

    Super, this video is enough to understand lifetime, thanks a lot ❤️❤️

  • @kiddkai
    @kiddkai 2 ปีที่แล้ว

    Great video, help me understand the lifetime annotation!

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

    Dude great explanation, I finally got the concept!

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

    very nice explanation. Very helpful.

  • @1234fewgfwe
    @1234fewgfwe 4 หลายเดือนก่อน

    My first time learning about lifetime in rust. I didn’t think it’s that difficult. In fact, it’s common sense. It’s about preventing dangling reference, meaning a reference to something that doesn’t exist anymore, which is a memory issue. Explicitly writing the tick generic lifetime annotation actually makes programmer think smarter about avoiding referencing something that may not live long enough in the program. It’s all about lifetime and it’s really that simple. Anything to do with reference, be careful with its lifetime, because you might reference to invalid memory.
    And some rules to follow, if only one reference parameter, there’s no need to write the lifetime annotation because definitely your output reference depends on it. If more than one, then you have to write the annotation and borrow checker will be able to determine, such that output reference lifetime is the smallest of the two parameters lifetime. Simple.

  • @CountJamesMcGuigan
    @CountJamesMcGuigan 2 ปีที่แล้ว +1

    Thank you for your tutorial. As a small piece of feedback for future videos, the faint music in the background was slightly distracting.

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

      It seems to be a trend in programming videos lately, and it is VERY distracting. I often find myself pausing the video thinking it’s from elsewhere in my house. I really wish these otherwise talented content creators would ditch the pointless and distracting background audio - it almost ruins what would otherwise be an excellent resource.

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

    This was amazingly helpful!

  • @tourdesource
    @tourdesource 2 ปีที่แล้ว

    Clear as day. Thank you!

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

    намного понятнее чем в книжках. Спасибо)

  • @lee45283
    @lee45283 3 ปีที่แล้ว +2

    Great video, thanks for the content!

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

    Ok, I understand half of this. What I don't understand is e.g. why I need to add /'a/ so many times. Why for instance do we have to add it in angle brackets after the function name? The simple answer to this would be "because Rust requires it", but I'm interested in the longer answer to "why does Rust require it? Is there anything that wouldn't work if in a language Rust2.0 only the input and output arguments had lifetime annotations, but not the function itself?". What I don't understand either is why x and y have the same lifetime annotation, even though we just said x and y can have different lifetimes. And could you show an example where we need more than one lifetime specification, e.g. /'a/ and /'b/? Thanks for these otherwise extremely helpful videos :)

    • @healthytips4818
      @healthytips4818 2 ปีที่แล้ว

      hopefully someday rust just use AI to generate this lifetime annotation :D

    • @swapode
      @swapode 2 ปีที่แล้ว +1

      x and y have the same lifetime annotation because the return value's lifetime depends on both of them. It doesn't matter that x and y can have different lifetimes, it's important that the values of both x and y outlive the return value, since the return value can be a pointer to either one.
      I made a simple modification to the longest() function, to demonstrate how two different lifetimes might be useful:
      fn weird_longest(x:&'a str, y:&'b str) -> Result {
      if x.len() > y.len() {
      Ok(x)
      }
      else {
      Err(y)
      }
      }
      While this also would compile with a single lifetime, it'd mean different things on the call side. Two different lifetimes would allow us to discard one of the actual values, while with the same lifetime we'd have to keep both of them around until the return value goes out of scope.
      I'm actually not quite sure about "why does Rust require it?". I can't come up with a counter example on the spot, something to think about. One reason might be something like monomorphization, explicitly declaring the lifetimes makes it clear that this is a generic function that might be compiled several times for different calls (what if both parameters have static lifetime?) but I can't quite wrap my head around it.

  • @marinhobrandao
    @marinhobrandao 9 หลายเดือนก่อน

    I loved to know you like The Wheel of Time as well :D

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

    Rust compiler writes me... I'm not writing the code...

  • @Jordan4Ibanez
    @Jordan4Ibanez 2 ปีที่แล้ว

    I wish I could like this video more than once

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

    Very nice explanination!!!!

  • @vijaysinghbisht3016
    @vijaysinghbisht3016 2 ปีที่แล้ว

    Thank you very much.. Explained very nicely..
    Can you do a day event?

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

    I spent an afternoon trying to figure out how lifetimes works, finally understood after watching this vid 🤦🏻‍♂️

  • @NoNameEntered
    @NoNameEntered 2 ปีที่แล้ว

    Great video and examples! Thank you!

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

    Very nicely done. Thank you 🙏

  • @thebsv
    @thebsv 8 หลายเดือนก่อน

    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?

  • @mr.bulldops7692
    @mr.bulldops7692 2 ปีที่แล้ว

    Lifetimes being relational is a revelation.

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

    very good man, your videos is easy for beginer like me.❤👍

  • @dadestor
    @dadestor 2 ปีที่แล้ว

    Great video and explanation, thanks

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

    9:28 lifetime for string literal will be considered(as we are passing string2,as_str(), and it's lifetime is static, then why there is error

  • @jacques-dev
    @jacques-dev 2 ปีที่แล้ว

    Wowie. Thanks Bogdan!

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

    Does this still apply, or are compilers more advanced now that we don't need to use generic lifetime annotations anymore?

  • @JhonatanMorais
    @JhonatanMorais 2 ปีที่แล้ว

    GREAT video. thank you so much.

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

    This is great explanation. Love it. Thanks.

  • @MikeM8891
    @MikeM8891 2 ปีที่แล้ว

    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.

  • @kvasco-iu8nq
    @kvasco-iu8nq 3 ปีที่แล้ว

    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 ?

  • @AlanDarkworld
    @AlanDarkworld 2 ปีที่แล้ว +13

    Whoever decided to use single quotes in a non-pairwise fashion in the Rust syntax just wants to see the world burn.

  • @LimitedWard
    @LimitedWard 3 ปีที่แล้ว +2

    This was really helpful. So far this seems to be the "ugliest" part of Rust's syntax, though, I guess it's just a direct consequence of having a borrow checker.
    What's the general consensus on the use of lifetime annotations? Should we be writing code to explicitly avoid needing them whenever possible?

    • @Daxlo
      @Daxlo 2 ปีที่แล้ว

      I'd love it if someone who knows could answer this question! I think they become important when you have references in multiple data structures. For example, two Vectors contain a reference to the same value.

    • @kevinmcfarlane2752
      @kevinmcfarlane2752 2 ปีที่แล้ว

      What tends to happen with languages is that they simplify the syntax over time. Though in the meantime they add more concepts with more syntax. Lol.