Updated src/generic_types_traits_and_lifetimes/traits.md'.

This commit is contained in:
Hector PENG
2026-03-22 16:20:29 +08:00
parent 544f0ce80d
commit d6b64ff021
6 changed files with 64 additions and 39 deletions

View File

@@ -0,0 +1,6 @@
[package]
name = "aggregator"
version = "0.1.0"
edition = "2024"
[dependencies]

View File

@@ -11,19 +11,19 @@ pub struct NewsArticle {
impl Summary for NewsArticle {
fn summarize(&self) -> String {
format!("{}, by {} ({})", self.headline, self.author, self.location)
format! ("{}, by {} ({})", self.headline, self.author, self.location)
}
}
pub struct Tweet {
pub struct SocialPost {
pub username: String,
pub content: String,
pub reply: bool,
pub retweet: bool,
}
impl Summary for Tweet {
impl Summary for SocialPost {
fn summarize(&self) -> String {
format!("{}: {}", self.username, self.content)
format! ("{}: {}", self.username, self.content)
}
}

View File

@@ -0,0 +1,14 @@
use aggregator::{SocialPost, Summary};
fn main() {
let post = SocialPost {
username: String::from("horse_ebooks"),
content: String::from(
"当然,跟大家已经知道的一样,朋友们",
),
reply: false,
retweet: false,
};
println!("1 个新帖子: {}", post.summarize());
}