mirror of
https://github.com/gnu4cn/rust-lang-zh_CN.git
synced 2026-08-19 12:43:28 +08:00
Updated src/generic_types_traits_and_lifetimes/traits.md'.
This commit is contained in:
6
projects/trait_demo/Cargo.toml
Normal file
6
projects/trait_demo/Cargo.toml
Normal file
@@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "aggregator"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
14
projects/trait_demo/src/main.rs
Normal file
14
projects/trait_demo/src/main.rs
Normal 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());
|
||||
}
|
||||
Reference in New Issue
Block a user