Rust: Generics, Traits, Lifetimes

preview_player
Показать описание
#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.

Рекомендации по теме
Комментарии
Автор

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

johnyepthomi
Автор

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

tylerhanson
Автор

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.

antoniong
Автор

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!

martinbecker
Автор

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

krome
Автор

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.

bla_blub
Автор

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

fallow
Автор

keep posting videos. I really like them!!

dulranga_
Автор

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

joeystenbeck
Автор

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

PatrickLemiuex
Автор

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

SavagePlay
Автор

Awesome video. You teach very well, and this really made generics "click" with me. Thanks.

I believe I was your 1, 000th subscriber too!

jacksystems
Автор

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.

steffahn
Автор

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

bjugdbjk
Автор

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.

topcivilian
Автор

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

yotubecreators
Автор

Thank you! You explain everything well!

李晧暐
Автор

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

AMusset
Автор

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

paulycdong
Автор

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.

flipper