Skip to content

Day 10 - Trait ​

定义Trait ​

rust
trait Summary {
    fn summarize(&self) -> String;
}

实现Trait ​

rust
struct Article {
    title: String,
    author: String,
}

impl Summary for Article {
    fn summarize(&self) -> String {
        format!("{} by {}", self.title, self.author)
    }
}

默认实现 ​

rust
trait Summary {
    fn summarize(&self) -> String {
        String::from("(Read more...)")
    }
}

📅 学习进度 ​

Day主题状态
01-09基础✅
10Trait✅
11闭包⬜

本文为Rust 30天学习计划第10天内容 ​

📚 导航 ​

| ← Day 09 - 上一章 | Day 11 - 下一章 →

最后更新: