Browse Source

Merge pull request 'dev' (#5) from dev into master

Reviewed-on: #5
master
jingxun 3 years ago
parent
commit
d692387f53
  1. 13
      day08/generic_day08/src/main.rs
  2. 8
      day08/trait_day08/Cargo.toml
  3. 58
      day08/trait_day08/src/lib.rs
  4. 30
      day08/trait_day08/src/main.rs

13
day08/generic_day08/src/main.rs

@ -25,6 +25,15 @@ impl<T> Point<T> {
}
}
impl<T, U> NewPoint<T, U> {
fn mixup<V, W>(self, other: NewPoint<V, W>) -> NewPoint<T, W> {
NewPoint {
x: self.x,
y: other.y,
}
}
}
fn main() {
let num_list = vec![1, 5, 2, 8, 3, 9, 3, 0, 4, 7];
let res = largest_i32(&num_list);
@ -35,9 +44,11 @@ fn main() {
let res = largest_char(&char_list);
println!("{}", res);
let integer = Point { x: 5, y: 10 };
let float = Point { x: 5.5, y: 10.5 };
let mix = NewPoint { x: 5, y: 10.5 };
println!("{}", integer.x());
let mix2 = NewPoint { x: 7, y: 2.5 };
let new = mix.mixup(mix2);
println!("{}{}", new.x, new.y)
}
fn largest_i32(list: &[i32]) -> i32 {

8
day08/trait_day08/Cargo.toml

@ -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]

58
day08/trait_day08/src/lib.rs

@ -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());
}

30
day08/trait_day08/src/main.rs

@ -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…
Cancel
Save