Rust Linz, July 2021 - Rainer Stropek - Traits, not your grandparents' interfaces

preview_player
Показать описание
Developers who approach Rust from the viewpoint of other OO languages like TypeScript, C#, or Java frequently misinterpret Rust's traits as interfaces. That might be true to a certain extent, but on closer examination traits are much more powerful than you might think. In this demo-only session, Rainer Stropek starts by introducing you to the fundamentals of traits. You will see how to create your own ones, how to manually implement system traits, and how to auto-implement them. Once we covered those basics, Rainer dives deeper into unique features of traits. We will look at default implementations, trait parameters, trait objects, and trait bounds for generics.

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

Rainer seems like a great guy. Helpful, clear, and humble.

CompletelyCovered
Автор

Awesome! The speaker has a great passion for Rust.

建平許
Автор

Nice so far.

Just a small nitpick, at 13:20: Pretty sure that "Auto-Implementation" is already (pretty much) used for something else, and even ignoring that thing there's other mechanisms that could be named the same.

Auto-Trait-Implementations is reserved for things that a user usually doesn't interface with directly, stuff like Sync (which gets imemented automatically on any type that is made up out of elements that implement Sync), Send etc.

These get implemented entirely without user interaction, while the things refered to in the talk still need to be explicitly opted in, even if that is with minimal boilerplate.

These traits refered to in the talk, eg Clone, Copy, Hash etc, are called "Derivable traits".

There's also YET ANOTHER mechanism, called "Banket Implementations". These are also powerful and interesting. One example is the Conversion Traits "From" and "Into", which can handle eg conversions between &str and String. As the documentation of these mentions you usually only want to Implement "From" on the target type, this will automatically create an implementation of "Into" on the source type via Blanket Implementation. So, if you implement "From<&str> for String" the compiler will generate a perfect implementation of "Into<String> for &str".

SMTM
Автор

I’d like to take a moment to say that a tied tic-tac-toe match is called a “cat’s game”

thalianero
Автор

The essential ideas behind the traits are explained by a passioned developer. Thank you, this is the first time that I see a trait is more than an interface.

budokan
Автор

I love your energy Rainer. That’s a great talk that I will recommend.

robinmoussu
Автор

I was confused by the `From`/`into` until it dawned on me that you are _not_ doing a reverse operation! It was subtle for me. Using pseudo-code, `Widget::From(1)` can simply be re-stated as `1.into()` which returns the same widget. This way, its a different syntax for writing the same thing that can come handy in some situation, e.g more ergonomic api with chained operation.

(This would be very different from say `Widget.into()` to return a 1 but thankfully thats not whats happening here. )

Amapramaadhy
Автор

15:30 according to Rust docs; use From for imlementation and use Into for trait bounds.

Examples:

Implementing From you can see in the video:
impl From<u8> for SquareContent { ... }

Trait bounds for Into only accepts parameters that are castable to SquareContent. we implemented From<u8> earlier so we can pass u8 because we know how to make SquareContent from u8;
fn my_super_function<T>(param: T) -> SquareContent
where
T: Into<SquareContent>,
{
...
}

yapayzeka
Автор

This is the most enthusiasm i Have ever seen for someones cause

pyrysaarinen
Автор

loved this! genuinely helped me understand traits better

irlshrek
Автор

This is a very in-depth video about rust features. Thanks!

sumansaha
Автор

Thank you so much. I filled some gaps about traits by watching the video.

marko-lazic
Автор

Rust is so ahead of the curve, as a C# guy I’m kinda jealous

willinton
Автор

Wait, did you said Ferris cake? I want one please

ikhlasulkamal
Автор

Oh, man. I need to learn German to understand traits? XD

Just kidding. Awesome talk. I would recomend it to all people looking into learning Rust.

luizchagasjardim
Автор

Awesome explanation for C# coming developers... from C# developer !!.

saaddahmani
Автор

I'm definitely confused with implementing into_iter. I'm understanding that you can add "parameters" to interfaces in some sense. Essentially, only as types.

All I can see from the example is a daunting pile of various versions of "into" and "iter", with various joining characters.

It makes sense to inform the compiler that the calling code will get an item of a specific type when iterating. It makes sense to reuse the into_iter implemented on an array. I'm having difficulty understanding the function.

If I'm understanding the code right, we're saying we're going to return the same type that calling into_iter on an array<SquareContent> of length 9 would return? Are these identical?





Zaniahiononzenbei
Автор

Can someone give me the German at 33:16 when talking about From implying Into?

erc_io
Автор

The speaker is awesome. 🤓. I really liked the code examples.

victorfds
Автор

Thank you very much for this very clear talk.

funcder