rust_project_demo/
lib.rs

1use std::fmt::{Debug, Display};
2
3
4pub trait Summary {
5    fn summarize(&self) -> String {
6        format!("This is default summarize impl {}", self.summarize_author())
7    }
8    fn summarize_author(&self) -> String;
9}
10
11pub struct Tweet {
12    pub username: String,
13    pub content: String,
14}
15
16pub struct NewArticle {
17    pub author: String,
18    pub content: String,
19}
20
21impl Summary for NewArticle {
22    fn summarize_author(&self) -> String {
23        format!("This is new article summarize {},{}", self.author, self.content)
24    }
25}
26
27impl Summary for Tweet {
28    fn summarize_author(&self) -> String {
29        format!("This is Tweet summarize {},{}", self.username, self.content)
30    }
31}
32
33pub fn notify1(item: impl Summary) {
34    println!("This is {} summarize: ", item.summarize_author());
35}
36
37// use multipart impl
38pub fn notify11(item: impl Summary + Display) {
39    println!("This is {} summarize: ", item.summarize_author());
40}
41
42// trait bound write method
43pub fn notify2<T: Summary>(item: T) {
44    println!("This is {} summarize: ", item.summarize_author());
45}
46
47// use multipart impl
48pub fn notify21<T: Summary + Display>(item: T) {
49    println!("This is {} summarize: ", item.summarize_author());
50}
51
52// use multipart impl
53pub fn notify3<T, U>(item1: T, item2: U)
54    where
55        T: Summary + Display,
56        U: Clone + Debug,
57{
58    println!("This is {} summarize: ", item1.summarize_author());
59}
60
61
62
63