jingxun
3 years ago
4 changed files with 108 additions and 1 deletions
@ -0,0 +1,8 @@ |
|||
[package] |
|||
name = "trait_day08" |
|||
version = "0.1.0" |
|||
edition = "2018" |
|||
|
|||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html |
|||
|
|||
[dependencies] |
@ -0,0 +1,58 @@ |
|||
/* |
|||
** encoding: utf-8 |
|||
** @author: Jinguxn |
|||
** @contact: chenjin.qian@xquant.com |
|||
** jingxun@lynchow.com |
|||
** @file: lib.py |
|||
** @create-time: 2021-11-29 15:09 |
|||
** @last-modified: 2021-11-29 15:09 |
|||
** @description: Input here. |
|||
*/ |
|||
|
|||
pub struct NewsArticle { |
|||
pub headline: String, |
|||
pub location: String, |
|||
pub author: String, |
|||
pub content: String, |
|||
} |
|||
|
|||
pub struct Tweet { |
|||
pub username: String, |
|||
pub content: String, |
|||
pub reply: bool, |
|||
pub retweet: bool, |
|||
} |
|||
|
|||
/*impl Summary for NewsArticle {
|
|||
fn summarize(&self) -> String { |
|||
format!("{}, by {} ({})", self.headline, self.author, self.location) |
|||
} |
|||
}*/ |
|||
|
|||
impl Summary for NewsArticle { |
|||
fn summarize_authort(&self) -> String { |
|||
format!("@{}", self.author) |
|||
} |
|||
} |
|||
|
|||
impl Summary for Tweet { |
|||
fn summarize_authort(&self) -> String { |
|||
format!("@{}", self.username) |
|||
} |
|||
|
|||
fn summarize(&self) -> String { |
|||
format!("{}:{}", self.username, self.content) |
|||
} |
|||
} |
|||
|
|||
pub trait Summary { |
|||
fn summarize_authort(&self) -> String; |
|||
|
|||
fn summarize(&self) -> String { |
|||
format!("(Read more from {}...)", self.summarize_authort()) |
|||
} |
|||
} |
|||
|
|||
pub fn notify(item:impl Summary){ |
|||
println!("Breaking news! {}", item.summarize()); |
|||
} |
@ -0,0 +1,30 @@ |
|||
/* |
|||
** encoding: utf-8 |
|||
** @author: Jinguxn |
|||
** @contact: chenjin.qian@xquant.com |
|||
** jingxun@lynchow.com |
|||
** @file: main.py |
|||
** @create-time: 2021-11-29 15:09 |
|||
** @last-modified: 2021-11-29 15:09 |
|||
** @description: Input here. |
|||
*/ |
|||
|
|||
use trait_day08::{Tweet, Summary, NewsArticle, notify}; |
|||
|
|||
fn main() { |
|||
let tweet = Tweet { |
|||
username: String::from("jingxun"), |
|||
content: String::from("of course, as you probably alread know, people"), |
|||
reply: false, |
|||
retweet: false, |
|||
}; |
|||
println!("1 new tweet: {}", 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 Pittsubrgh Penguins once again are the best hockey team in the NHL."), |
|||
}; |
|||
println!("New article available! {}", article.summarize()); |
|||
notify(article); |
|||
} |
Loading…
Reference in new issue