summaryrefslogtreecommitdiff
path: root/rust/theBook/chapter-10-generic-types-traits-lifetimes/traits/src/lib.rs
blob: 45c93c8a88c2daff25173bc19de51f3a0f5ee952 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
pub trait Summary {
    fn summarize (&self) -> String;
}

pub struct NewsArticle {
    pub author: String,
    pub headline: String,
    pub location: String,
    pub content: String,
}

impl Summary for NewsArticle {
    fn summarize(&self) -> String {     // return String ownership. &self means self: &Self
        format!("{} by {} ({})", self.headline, self.author, self.location)
    }
}

pub struct Tweet {
    pub username: String,
    pub content: String,
    pub retweet: bool,
    pub reply: bool,
}

impl Summary for Tweet {
    fn summarize(&self) -> String {     // &Self is the type owns impl
        format!("{}: {}",self.username, self.content)
    }
}