Back to course

Traits: Defining Shared Behavior

Rust for Systems & Web3 Security

What are Traits?

A trait tells the Rust compiler about functionality a particular type has and can share with other types. They are similar to interfaces in other languages.

rust pub trait Summary { fn summarize(&self) -> String; }

Trait Bounds

You can constrain generics so they only work with types that implement specific traits (e.g., T: Display).