Menu

[Rust Guide] 10.3. Trait Pt.1 - Trait Definitions, Bounds, and Implementation
📰

[Rust Guide] 10.3. Trait Pt.1 - Trait Definitions, Bounds, and Implementation

DEV Community·SomeB1oody·6 months ago
#2NfcXzRW
#rust#programming#learning#software#trait
Reading 0:00
15s threshold

If you find this helpful, please like, bookmark, and follow. To keep learning along, follow this series.

10.3.1 What Is a Trait

Trait means feature or characteristic. Traits are used to describe to the Rust compiler what capabilities a type has and which behaviors it can share with other types . Traits define shared behavior in an abstract way.

There is also the concept of trait bounds, which can constrain a generic type parameter to a type that implements a specific behavior . In other words, it requires the generic type parameter to implement certain traits.

Traits in Rust are somewhat similar to interfaces in other languages , but there are still differences.

10.3.2 Defining a Trait

The behavior of a type is made up of the methods that the type itself can call. Sometimes different types have the same methods, and in that case we say those types share the same behavior.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.