From the course: Rust Essential Training

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Default trait implementation

Default trait implementation - Rust Tutorial

From the course: Rust Essential Training

Start my 1-month free trial

Default trait implementation

- [Instructor] In certain situations, it may be useful to have a default implementation for one or more of the methods in a trait. That could be especially useful if you have a trait with a large number of methods and you don't want to have to implement every single one of them for every single data type with that trait. You can provide a default implementation as part of the trait definition. We'll do that here for our describe method on line 13 by replacing the semicolon at the end of the method signature with curly braces. Then within that code block, we'll create a new string, and we'll give it a generic description that this is an object flying through space. Now, both of our structs are currently implementing their own unique versions of the describe method, which will override or replace the default version we just defined. So they're individually implemented versions of describe or what will actually get used. But…

Contents