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
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!("The largest member is x = {}", self.x);
        } else {
            println!("The largest member is y = {}", self.y);
        }
    }
}
trait Summary {
    fn summarize(&self) -> String;
}

struct Tweet {
    pub content: String,
    pub autor: String,
}

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

struct Article {
    pub headline: String,
    pub location: String,
    pub author: String,
}

impl Summary for Article {
    fn summarize(&self) -> String {
        format!(
            "{} by {}, locate: {}",
            self.headline, self.author, self.location
        )
    }
}

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

pub fn traits_usage_study() {
    let article = Article {
        headline: String::from("article headline"),
        location: String::from("canada"),
        author: String::from("clare"),
    };
    println!("article summary {}", article.summarize());
    notify(&article);
}