Rust: Generics, Traits, Lifetimes

Поділитися
Вставка
  • Опубліковано 23 чер 2024
  • #rust #generic #traits #lifetime
    Generics are like a placeholder to another type. Think of a can with food in it. That can is concrete but the food inside is a generic type. The can have soup, beans, corn, or almost anything else inside it.
    We have already been using generics with the `Option` type and `Result` type from the error handling chapter.
    Here, we will discover how to use generics with our own functions, structs, and methods.
    Traits are used to define behavior in a generic way.
    And, finally, you cannot talk about generics without talking about lifetimes. This is Rust feature has probably popped up for you in compiler error messages.
    Links
    Chapter 17 of the Rust Programming book as it relates to the discussion.
    doc.rust-lang.org/book/ch17-0...
    For more complex scenarios involving lifetimes, there’s additional content in the “Rust Reference”. doc.rust-lang.org/reference/t...
  • Наука та технологія

КОМЕНТАРІ • 55

  • @tylerhanson133
    @tylerhanson133 2 роки тому +48

    Very helpful. It's hard to find detailed videos on Rust by experienced engineers in it.

    • @ccgarciab
      @ccgarciab Рік тому +3

      Look up Jon Gjengset

    • @michaelshort2388
      @michaelshort2388 Рік тому

      not a video, but you can also find the explanation in the rust book. :)

  • @johnyepthomi892
    @johnyepthomi892 Рік тому +5

    It’s people like you that will increase rust adoption. Thank you for this style of explanation. Very concise and clear with examples.

  • @antoniong4380
    @antoniong4380 7 місяців тому +4

    For those who haven't figured when to employ monomorphism or polymorphism yet, if you ever feel like the binary size might go crazy big due to monomorphization and you want to keep it small, use the Dyn keyword, and sacrifice some runtime performance so you can get smaller binary.
    How can you know when it might go crazy big? (Might be wrong, but I shouldn't be far)
    if your function has 1 Generics, the max amount of variants for that specific function is X^1, where X is the all the types that you used the generic function with. For example, you use the generic function with f64 and i64, you will get 2 variants.
    if you use a Function with 4 generics, the max amount of variants that might be generated is X^4.
    So if you ever feel constrained by binary size, fixing monomorphization on generics might be a place you want to look to fix.

  • @bla_blub
    @bla_blub Рік тому +7

    15:54 The generic form implies, that item 1 and item2 are of exactly the same type, while the function on line 36 can have arguments of two different types, as long as they implement Summary. Notify needs a second generic.

  • @martinbecker1069
    @martinbecker1069 Рік тому +5

    This was super helpful! Whilst I've always understood the lifetimes concept but I've always struggled to grok the lifetime syntax.
    This was a massive help in understanding it, thank you!

  • @krome305
    @krome305 Рік тому +2

    This is the best and detailed tutorial about Generics, Traits and Lifetimes that I've found, thank you!

  • @fallow64
    @fallow64 Рік тому

    Thank you so much for including the specific chapter in the rust handbook!

  • @bsd0daemon
    @bsd0daemon Рік тому +1

    This was really good. It covers things I feel I already know, but each time it is covered, it is like learning it again. It solidifies the knowledge. I guess I never really knew the Elision rules so was always a bit confused about when I need to specify explicit lifetimes.
    The quote "lifetimes are very much a tool to get rid of dangling pointers" really stuck with me. I felt like I knew that but I guess not that well, but it is now in the forefront of my mind.

  • @bjugdbjk
    @bjugdbjk Рік тому

    Fantastic man, Really enjoying the playlist, can see the difference how experiences programmers talk about and explain the concepts of rust !! really looking for the next !!
    Could you do a dedicated video on static generic lifetimes !!

  • @dulranga_2
    @dulranga_2 2 роки тому +4

    keep posting videos. I really like them!!

  • @user-ex8vj4qe7n
    @user-ex8vj4qe7n Рік тому

    Thank you! You explain everything well!

  • @PatrickLemiuex
    @PatrickLemiuex Рік тому

    you are great annunciator. very clearly explained and easy on the ears

  • @joeystenbeck6697
    @joeystenbeck6697 Рік тому +2

    Hey Riki! (Ricki, Ricky, …?) Awesome videos. You explain things in a very clear and engaging way. I look forward to watching more of your Rust videos. I have a small suggestion- when pasting something (e.g., the generic version at time: 2:57), I think it may be helpful to not delete the nongeneric code in the process, so that generic and nongeneric can be compared side-by-side. Thanks for making these, they’re great!
    - Joey

  • @SavagePlay
    @SavagePlay 11 місяців тому

    thank you this was very helpful with detail explanation pls never stop making more

  • @rmvlvs5228
    @rmvlvs5228 8 місяців тому

    *THIS VIDEO IS SPECTACULAR AND EVERYONE SHOULD WATCH IT*

  • @jacksystems
    @jacksystems Рік тому

    Awesome video. You teach very well, and this really made generics "click" with me. Thanks.
    I believe I was your 1,000th subscriber too!

  • @user-jm2gu4zb7c
    @user-jm2gu4zb7c Рік тому

    Thanks a bunch, great video!

  • @steffahn
    @steffahn Рік тому +1

    Something about the shape of the U!? No, man, it's just the next letter in the alphabet after T. 😅
    (Similarly, S is common because it's the letter before T.)
    Admitted, the similarity of U and V might be a reason why R, S, T could be preferred over T, U, V if you need more types; though for very many single-letter types it can also make sense to start with A, B, C, to have more room in the alphabet, or use T1, T2, T3, if you like numbers.

  • @AMusset
    @AMusset 5 місяців тому

    Thank you for the video. You got a new sub!
    Which extension are you using to see scopes with the yellow line?

  • @b1chler
    @b1chler Рік тому

    Cool stuff! Thanks

  • @danraine9009
    @danraine9009 Рік тому

    Thank you for this video, learned a lot and opened my mind to more possibilities for coding solutions :)

  • @prashlovessamosa
    @prashlovessamosa Рік тому

    Thank you

  • @topcivilian
    @topcivilian Рік тому

    Excellent video, my friend. Very informative.
    The google algorithm brought me here.
    You just got a new subscriber.
    Looking forward to more content like this.

  • @yotubecreators47
    @yotubecreators47 10 місяців тому

    Wow this is perfect explanation for me, except Lifetimes explanation seems was for advanced Rustlers, thanks a lot

  • @johnstorm589
    @johnstorm589 Рік тому

    Mind-bending, but useful!

  • @agungokill
    @agungokill Рік тому

    ayt, this is very nice and brief explanation

  • @flipper71100
    @flipper71100 Рік тому

    It was a wonderful video I totally get the concept of generics and trait, however some part of the lifetimes are still not clear to me.

  • @paulycdong
    @paulycdong Рік тому +1

    Great job! You probably have already covered this somewhere else but I really like the extension in your VS Code that highlights the brackets location. Can you share the name? Thank you!😊

    • @IvanAndreScheel
      @IvanAndreScheel Рік тому +1

      In case you're still looking. It's built into VS Code itself. Look for bracket pair guides in the editor config.

    • @mariozulmin
      @mariozulmin 3 місяці тому

      @@IvanAndreScheel thx, was looking for that, right in the moment

  • @MohammadRajablooloverajabloo

    Thank you. what is the solution for `longest` in 26:05 (returning a dropd value), I encounter this problem and couldn't find the best solution.

  • @lucasfkam
    @lucasfkam 9 місяців тому

    What is the name of the fonttype you are using in this video?

    • @TheDevMethod
      @TheDevMethod  7 місяців тому

      I love using Blex Mono Nerd Font. It's a patched version of Plex Mono by IBM's Carbon deign

  • @_luisgerardo
    @_luisgerardo Рік тому

    Good

  • @HariRoshan93
    @HariRoshan93 2 роки тому

    Love it

  • @jameszack7158
    @jameszack7158 10 місяців тому

    How would you ever create an application in a non-object-oriented programming language? It seems impossible. You need those handy classes, methods, properties and encapsulations...

  • @mazinayman4004
    @mazinayman4004 9 місяців тому

    Which colorscheme you use?

    • @TheDevMethod
      @TheDevMethod  7 місяців тому

      I switch off between a couple. I use GitHub Dark High Contrast but this video might be GitHub Dark without the High Contrast.

  • @curioSEOty
    @curioSEOty Рік тому +1

    trying to fix 5:11 but it gives out more and more errors. would've been nice to see the stuff running before you moved on to the next thing

    • @bertybates705
      @bertybates705 Рік тому

      ua-cam.com/video/JLfEiJhpTbE/v-deo.html

  • @overclucker
    @overclucker 3 місяці тому

    T stands for c++ Template, haha. The first example is one step away from a linear sort.

  • @jonathanmoore5619
    @jonathanmoore5619 Рік тому

    I'll do what I want to do.

  • @malusmundus-9605
    @malusmundus-9605 4 місяці тому

    Ah it's c++ templates again... for when void* isn't good enough. Except, it's not a void pointer in the way it functions... this is function overloading.

  • @backupmemories897
    @backupmemories897 Рік тому

    its like template at c++ i see

  • @matthiasg4843
    @matthiasg4843 Рік тому +1

    Did you just copy the code examples 1:1 from Let's Get Rusty?

    • @TheDevMethod
      @TheDevMethod  11 місяців тому +2

      The examples are from the Rust Programming Book. The link is in the description if you would like to take a look and follow along.

    • @matthiasg4843
      @matthiasg4843 11 місяців тому

      @@TheDevMethod Sorry for the misunderstanding. I watched 2 vids after another and saw the same code examples

  • @SpeckyYT
    @SpeckyYT Рік тому

    Right, do not duplicate your code! Better create a macro that duplicates it for you 😂

  • @dragonmax2000
    @dragonmax2000 2 роки тому +1

    Quite a bit of detail is missing. The video does go into some detail, but a bit shallow. I would have expected to see unit tests and implementations that are complete. Please keep it going, but add some more additional context. Thank you!

  • @dmm10000
    @dmm10000 Рік тому

    "Borrowed" is an utterly confusing stupid term - wouldnt a simple "out of scope" be much better