summaryrefslogtreecommitdiff
path: root/traits/src/main.rs
blob: 770caa9fb46e28569472d00c2c4b3932f969bc5d (plain) (blame)
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
use std::fmt::Display;

struct Pair<T> {
    x: T,
    y: T,
}

impl<T> Pair<T> {
    fn new(x: T, y: T) -> Self {
        Self {
            x,
            y,
        }
    }
}

impl<T: Display + PartialOrd> Pair<T> {
    fn cmp_display(&self) {
        if self.x >= self.y {
            println!("{}", self.x);
        }
        else {
            println!("{}", self.y);
        }
    }
}

pub trait Summary {
    fn summarize(&self) -> String {
        format!("(read more from {}...)", self.summarize_author())
    }

    fn summarize_author(&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)
//    }

    fn summarize_author(&self) -> String {
        format!("{}", self.author)
    }

}

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

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

    fn summarize_author(&self) -> String {
        format!("@{}", self.username)
    }
}

pub fn notify(item: impl Summary) {
    println!("breaking news: {}", item.summarize());
}

fn returns_summarizable() -> impl Summary {

    NewsArticle {
        headline: String::from("Penguins win the stanley cup championship!"),
        location: String::from("Pittsburgh, PA, USA"),
        author: String::from("Iceburgh"),
        content: String::from("The pittsburgh penguins are winners I guess."),
    }
//    Tweet {
//        username: String::from("horse_ebooks"),
//        content: String::from("of course blargh"),
//        reply: false,
//        retweet: false,
//    }
}

fn main() {
    
    let tweet = Tweet {
        username: String::from("horse_ebooks"),
        content: String::from("of course, as you probably already know, people"),
        reply: false,
        retweet: false,
    };

    //dbg!(tweet.summarize());

    let article = NewsArticle {
        headline: String::from("Penguins win the stanley cup championship!"),
        location: String::from("Pittsburgh, PA, USA"),
        author: String::from("Iceburgh"),
        content: String::from("The pittsburgh penguins are winners I guess."),
    };

    //dbg!(article.summarize());

    //notify(tweet);

    returns_summarizable();
}