Rust Powered Polymorphism ⚡️ With Traits

preview_player
Показать описание
A quick overview of object oriented programming and polymorphism in Rust.

---

Stuff I use to make these videos - I absolutely love all of these products. Using these links is an easy way to support the channel, thank you so much if you do so!!!

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

This would've probably went over the scope of what you were trying to show but for those interested:

If you want anything that implements both `LandCapable` and `WaterCapable` to automatically implement `Amphibious` as well, you can do so:

impl<T: LandCapable + WaterCapable> Amphibious for T {}

Which can be read as "implement `Amphibious` for any type T that implements `LandCapable` and `WaterCapable`".

There is also alias traits in unstable, that basically do more or less the same thing but with less boilerplate:

trait Amphibious = LandCapable + WaterCapable;

Possseidon
Автор

Worth mentioning that `impl Trait` is just syntax sugar for `fn road_trip<T: LandCapable>(vehicle: &T) {}`

narigoncs
Автор

Worth mentioning that "prefer composition over inheritance" principle is general for any language. Rust just takes it as the standard and makes inheritance obsolete.

mkvoq
Автор

One place where you really cannot escape the dynamic dispatch is when you have mixed types like a vector that can contain both SUV and Sudan

Julian-vivl
Автор

I think it's important to note a naming convention for traits used in rust: if your trait implements one function it should be named the same way as the function. So in this example idiomatic names for LandCapable and WaterCapable would be "Drive" and "Float". It is nice to know the convention as many of the standard traits are named this way (for example From and IntoIterator)

BulbaWarrior
Автор

Your clear and concise way of explaining things really helps me to learn more about Rust. Looking forward to the next episode...Thank you!

narwodev
Автор

Was stuck wondering how to do something in a Rust project, and you just went ahead and put the solution on a platter for me. Excellent video, as always.

cd-bitcrshr
Автор

This aspect of Rust has been really hard for me to wrap my head around. Perhaps even more so than Thanks as always for the great content. This was helpful 😁

Kodlak
Автор

Absolutely love your videos. Simple examples straight to the point. Keep them coming! thanks

sergiuoanes
Автор

Whoa!! the way you explained the zero cost abstraction is mind blowing!!

fayaz
Автор

I was writing a ray tracer in rust and this video was super helpful in explaining a trait!

aditeya
Автор

Didn't realize &impl and &dyn were so interchangeable, thanks!

kippie
Автор

Every time I lookup something new that I'm learning, I have to wade through numerous videos with some guy talking forever about nothing and saying words I dont understand before finally stumbling across the quick and concise explanation that actually helps me. This time, I found it right away :)

ygypt
Автор

There are two very practical advantages of traits compared to inheritance base polymorphism: automatic implementation (if your type implements other specific traits), and automatic derivation. Because traits are seperate piece of code from your datatype it is ok to generate implementation for them automatically as this does not affect existing code base, compared to generating methods for interfaces.

ukaszlampart
Автор

This feels like a far superior way to do OOP than what I am used to see in other languages.

marcusrehn
Автор

At 7:28 I paused and said damn, this guy is really good at naming things.

sofroneageorgian
Автор

Just getting into Rust and this clarified a lot from me. Thanks.

wasabinator
Автор

This video helped me understand the difference between the dyn and impl syntax. Thank you!

creativecraving
Автор

Tnx! This actually helped me understand the trait system in rust. I know 'traits' from PHP, but I never found a situation where it could be useful (its just horizontal inheritance basically). The way Rust uses traits makes much more sense to me.

rct
Автор

Dynamic dispatch is also extremely good when combined with Reference-Counting f.e. "Rc<RefCell<MyType>>"
and combined with Any we can even do some limited downcasting

"prefer composition over inherintance" sure... but ngl... sometimes if you dont just have shared functionallity... but also shared data... it becomes a mess of delegations... which imo is not ideal...

Panakotta