Тёмный

Rust: Generics, Traits, Lifetimes 

The Dev Method
Подписаться 6 тыс.
Просмотров 44 тыс.
50% 1

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

Наука

Опубликовано:

 

16 июн 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 55   
@antoniong4380
@antoniong4380 7 месяцев назад
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.
@tylerhanson133
@tylerhanson133 Год назад
Very helpful. It's hard to find detailed videos on Rust by experienced engineers in it.
@ccgarciab
@ccgarciab Год назад
Look up Jon Gjengset
@michaelshort2388
@michaelshort2388 Год назад
not a video, but you can also find the explanation in the rust book. :)
@johnyepthomi892
@johnyepthomi892 Год назад
It’s people like you that will increase rust adoption. Thank you for this style of explanation. Very concise and clear with examples.
@bla_blub
@bla_blub Год назад
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 Год назад
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 Год назад
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 Год назад
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 !!
@joeystenbeck6697
@joeystenbeck6697 Год назад
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
@PatrickLemiuex
@PatrickLemiuex Год назад
you are great annunciator. very clearly explained and easy on the ears
@dulranga_2
@dulranga_2 2 года назад
keep posting videos. I really like them!!
@AMusset
@AMusset 4 месяца назад
Thank you for the video. You got a new sub! Which extension are you using to see scopes with the yellow line?
@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!
@SavagePlay
@SavagePlay 11 месяцев назад
thank you this was very helpful with detail explanation pls never stop making more
@user-ex8vj4qe7n
@user-ex8vj4qe7n Год назад
Thank you! You explain everything well!
@danraine9009
@danraine9009 Год назад
Thank you for this video, learned a lot and opened my mind to more possibilities for coding solutions :)
@user-jm2gu4zb7c
@user-jm2gu4zb7c Год назад
Thanks a bunch, great video!
@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.
@b1chler
@b1chler Год назад
Cool stuff! Thanks
@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.
@steffahn
@steffahn Год назад
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.
@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.
@rmvlvs5228
@rmvlvs5228 8 месяцев назад
*THIS VIDEO IS SPECTACULAR AND EVERYONE SHOULD WATCH IT*
@agungokill
@agungokill Год назад
ayt, this is very nice and brief explanation
@prashlovessamosa
@prashlovessamosa Год назад
Thank you
@yotubecreators47
@yotubecreators47 10 месяцев назад
Wow this is perfect explanation for me, except Lifetimes explanation seems was for advanced Rustlers, thanks a lot
@paulycdong
@paulycdong Год назад
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 Год назад
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
@johnstorm589
@johnstorm589 Год назад
Mind-bending, but useful!
@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
@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.
@HariRoshan93
@HariRoshan93 Год назад
Love it
@_luisgerardo
@_luisgerardo Год назад
Good
@curioSEOty
@curioSEOty Год назад
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 Год назад
ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-JLfEiJhpTbE.html
@overclucker
@overclucker 3 месяца назад
T stands for c++ Template, haha. The first example is one step away from a linear sort.
@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.
@matthiasg4843
@matthiasg4843 Год назад
Did you just copy the code examples 1:1 from Let's Get Rusty?
@TheDevMethod
@TheDevMethod 11 месяцев назад
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
@backupmemories897
@backupmemories897 Год назад
its like template at c++ i see
@jonathanmoore5619
@jonathanmoore5619 Год назад
I'll do what I want to do.
@SpeckyYT
@SpeckyYT Год назад
Right, do not duplicate your code! Better create a macro that duplicates it for you 😂
@dragonmax2000
@dragonmax2000 2 года назад
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!
@TheDevMethod
@TheDevMethod Год назад
Thank you for the feedback.
@dmm10000
@dmm10000 Год назад
"Borrowed" is an utterly confusing stupid term - wouldnt a simple "out of scope" be much better
Далее
Rust: Automated Testing
23:09
Просмотров 6 тыс.
"Type-Driven API Design in Rust" by Will Crichton
40:57
Best ASMR 😳
00:26
Просмотров 20 тыс.
Самый надежный автомобиль
01:00
Просмотров 609 тыс.
Tipuan Jenius dalam Mengasuh Anak & Gadget Cerdas
00:21
Rust: Closures
19:53
Просмотров 16 тыс.
but what is 'a lifetime?
12:20
Просмотров 59 тыс.
Why Rust is NOT a Passing Fad...
8:54
Просмотров 27 тыс.
Self-referential structs (in Rust)
27:21
Просмотров 51 тыс.
8 deadly mistakes beginner Rust developers make
14:14
Просмотров 156 тыс.
You Should Really Know These Traits in Rust
18:36
Просмотров 10 тыс.
Rust Lifetimes
26:52
Просмотров 50 тыс.
Implementing Rust Traits
12:19
Просмотров 13 тыс.
Урна с айфонами!
0:30
Просмотров 6 млн