mirror of
https://github.com/gnu4cn/rust-lang-zh_CN.git
synced 2026-08-22 14:13:28 +08:00
Initial commit.
This commit is contained in:
8
aggregator/Cargo.toml
Normal file
8
aggregator/Cargo.toml
Normal file
@@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "aggregator"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
3
aggregator/src/bin/test_case.rs
Normal file
3
aggregator/src/bin/test_case.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
fn main() {
|
||||
println! ("媒体聚合器");
|
||||
}
|
||||
56
aggregator/src/lib.rs
Normal file
56
aggregator/src/lib.rs
Normal file
@@ -0,0 +1,56 @@
|
||||
pub trait Summary {
|
||||
fn summarize_author(&self) -> String;
|
||||
|
||||
fn summarize(&self) -> String {
|
||||
format! ("(了解更多来自 {} ......)", self.summarize_author())
|
||||
}
|
||||
}
|
||||
|
||||
pub struct NewsArticle {
|
||||
pub headline: String,
|
||||
pub location: String,
|
||||
pub author: String,
|
||||
pub content: String,
|
||||
}
|
||||
|
||||
impl Summary for NewsArticle {
|
||||
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_author(&self) -> String {
|
||||
format! ("@{}", self.username)
|
||||
}
|
||||
}
|
||||
|
||||
use std::fmt::Display;
|
||||
|
||||
pub struct Pair<T> {
|
||||
pub x: T,
|
||||
pub y: T,
|
||||
}
|
||||
|
||||
impl<T> Pair<T> {
|
||||
pub fn new(x: T, y: T) -> Self {
|
||||
Self { x, y }
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Display + PartialOrd> Pair<T> {
|
||||
pub fn cmp_display(&self) {
|
||||
if self.x >= self.y {
|
||||
println! ("极大数为 x = {}", self.x);
|
||||
} else {
|
||||
println! ("极大数为 y = {}", self.y);
|
||||
}
|
||||
}
|
||||
}
|
||||
54
aggregator/src/main.rs
Normal file
54
aggregator/src/main.rs
Normal file
@@ -0,0 +1,54 @@
|
||||
use aggregator::{Summary, Tweet, NewsArticle, Pair};
|
||||
|
||||
pub fn notify<T: Summary>(item: &T) {
|
||||
println! ("突发新闻!{}", item.summarize());
|
||||
}
|
||||
|
||||
fn return_summarizable() -> impl Summary {
|
||||
Tweet {
|
||||
username: String::from("horse_ebooks"),
|
||||
content: String::from(
|
||||
"当然,如同你或许已经知道的一样,朋友们"
|
||||
),
|
||||
reply: false,
|
||||
retweet: false,
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let tweet = Tweet {
|
||||
username: String::from("horse_ebooks"),
|
||||
content: String::from(
|
||||
"当然,跟大家已经清楚的一样了,朋友们",
|
||||
),
|
||||
reply: false,
|
||||
retweet: false,
|
||||
};
|
||||
|
||||
println!("1 条新推文: {}", tweet.summarize());
|
||||
notify(&tweet);
|
||||
|
||||
|
||||
let article = NewsArticle {
|
||||
headline: String::from("企鹅队赢得斯坦利杯锦标赛!"),
|
||||
location: String::from("美国,宾夕法尼亚州,匹兹堡"),
|
||||
author: String::from("Iceburgh"),
|
||||
content: String::from(
|
||||
"匹兹堡企鹅队再度成为美国曲棍球联盟 \
|
||||
NHL 中的最佳球队。"
|
||||
),
|
||||
};
|
||||
|
||||
println! ("有新文章可读!{}", article.summarize());
|
||||
notify(&article);
|
||||
|
||||
println! ("1 条旧推文: {}", return_summarizable().summarize());
|
||||
|
||||
let pair = Pair::new(5, 10);
|
||||
pair.cmp_display();
|
||||
|
||||
let pair = Pair::new("这是一个测试", "This is a test.");
|
||||
pair.cmp_display();
|
||||
|
||||
println! ("{}", 3.to_string());
|
||||
}
|
||||
Reference in New Issue
Block a user