Menu

[Rust Guide] 10.4. Trait Pt.2 - Traits as Parameters and Return Types, Trait Bounds
📰
0

[Rust Guide] 10.4. Trait Pt.2 - Traits as Parameters and Return Types, Trait Bounds

DEV Community·SomeB1oody·about 1 month ago
#DQNgxSs4
Reading 0:00
15s threshold

If you find this helpful, please like, bookmark, and follow. To keep learning along, follow this series. By the way, writing this article took even longer than writing the ownership chapter. Traits are truly a concept that is hard to understand. 10.4.1 Using Traits as Parameters Let’s continue using the content from the previous article as the example: pub trait Summary { fn summarize ( & self ) -> String ; } pub struct NewsArticle { pub headline : String , pub location : String , pub author : String , pub content : String , } impl Summary for NewsArticle { fn summarize ( & self ) -> String { format! ( "{}, by {} ({})" , self .headline , self .author , self .location ) } } pub struct Tweet { pub username : String , pub content : String , pub reply : bool , pub retweet : bool , } impl Summary for Tweet { fn summarize ( & self ) -> String { format!…

Continue reading — create a free account

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

Read More