Rust Linz, October 2020 - Valentin Tolmer - How not to rely on inheritance

preview_player
Показать описание
How not to rely on inheritance
As opposed to many languages, Rust doesn't have a concept of inheritance, relying instead on Traits. In this talk, we'll talk about Traits and how to use them for common programming patterns.

About Valentin Tolmer
I am a craftsman, always working to improve my code and others' around me.

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

Just to be clear. The compiler is doing the monomorphization. You say copy/paste at one point that might confuse one into where this happens.

stephenjames
Автор

I am learning Rust about 3 years after learning it went on my project list, and these videos are invaluable. Thank you (if you still read comments on this video!). I'm extending some OpenAPI generated code for a Docker Registry which also allows Bittorrent distribution as my learning project. I hope to eventually have something that gains traction in the world. It's insane that there is not a P2P distribution for container images, especially inside the enterprise.

Melch
Автор

Very informative, Thank you, Really got a overview how powerful and simple the traits can do the things in rust which are little complex in other GC based langs like Java.

bjugdbjk
Автор

For the Mosquito type, I guess you can just leave out implementing the Adoptable trait.
Then you don't need the "if" in maybeAdopt(), since that function cannot bind to mosquitos (since they don't implement the Adoptable trait).

mihaigalos
Автор

Great talk! I wonder where I can find the slides?

HerringtonDarkholme
Автор

It is probably going to sound dumb but let's say I have:
trait Animal{
fn get_age(&self)-> i32;
}

struct Cat {
age: i32
}

impl Animal for Cat{
fn get_age(&self)-> i32{
return self.age;
}
}

struct Dog {
age:i32
}

impl Animal for Dog{
fn get_age(&self)-> i32{
return self.age;
}
}

//How can I write a function:
fn make_it_age<T:Animal>(a:T, years:i32) -> T{
//This must return the aged animal
}

nicolasmartino