How to implement a trait in Rust

Define a trait with required methods and use the impl keyword to provide those method bodies for a specific struct or type.

Implement a trait in Rust by defining the trait with required methods, then using the impl keyword to provide those methods for a specific type.

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

struct NewsArticle {
    headline: String,
}

impl Summary for NewsArticle {
    fn summarize(&self) -> String {
        format!("Read more: {}...", self.headline)
    }
}