From 5463a95d4ee9211ac56466993bf1704a96141428 Mon Sep 17 00:00:00 2001 From: sunface Date: Fri, 4 Mar 2022 17:59:59 +0800 Subject: [PATCH] update readme.md --- Readme.md | 3 ++ solutions/generics-traits/traits.md | 7 +++ src/generics-traits/traits.md | 75 +++++++++++++++++++++++++++++ src/why-exercise.md | 2 + 4 files changed, 87 insertions(+) create mode 100644 solutions/generics-traits/traits.md diff --git a/Readme.md b/Readme.md index db57f20..2e1a33b 100644 --- a/Readme.md +++ b/Readme.md @@ -12,6 +12,9 @@ This book was designed for easily diving into Rust,and it's very easy to use: - Difficulty from easy to super hard: easy 🌟 medium 🌟🌟 hard 🌟🌟🌟 super hard 🌟🌟🌟🌟 - Both [English](https://practice.rs) and [Chinsese](https://zh.practice.rs) are supported + +> Part of our examples and exercises are borrowed from [Rust By Example](https://github.com/rust-lang/rust-by-example), thanks for the great works you have been doing! + ## Some of our exercises 🌟🌟🌟 Tuple struct looks similar to tuples, it has added meaning the struct name provides but has no named fields. It's useful when you want give the whole tuple a name, but don't care the fields's names. diff --git a/solutions/generics-traits/traits.md b/solutions/generics-traits/traits.md new file mode 100644 index 0000000..6989dc4 --- /dev/null +++ b/solutions/generics-traits/traits.md @@ -0,0 +1,7 @@ +1. +```rust +``` + +1. +```rust +``` \ No newline at end of file diff --git a/src/generics-traits/traits.md b/src/generics-traits/traits.md index d3d6b44..f47281d 100644 --- a/src/generics-traits/traits.md +++ b/src/generics-traits/traits.md @@ -1,5 +1,80 @@ # Traits +A trait tells the Rust compiler about functionality a particular type has and can share with other types. We can use traits to define shared behavior in an abstract way. We can use trait bounds to specify that a generic type can be any type that has certain behavior. +> Note: Traits are similar to a feature often called interfaces in other languages, although with some differences. + +## Examples +```rust,editable +struct Sheep { naked: bool, name: String } + +trait Animal { + // Associated function signature; `Self` refers to the implementor type. + fn new(name: String) -> Self; + + // Method signatures; these will return a string. + fn name(&self) -> String; + + fn noise(&self) -> String; + + // Traits can provide default method definitions. + fn talk(&self) { + println!("{} says {}", self.name(), self.noise()); + } +} + +impl Sheep { + fn is_naked(&self) -> bool { + self.naked + } + + fn shear(&mut self) { + if self.is_naked() { + // Implementor methods can use the implementor's trait methods. + println!("{} is already naked...", self.name()); + } else { + println!("{} gets a haircut!", self.name); + + self.naked = true; + } + } +} + +// Implement the `Animal` trait for `Sheep`. +impl Animal for Sheep { + // `Self` is the implementor type: `Sheep`. + fn new(name: String) -> Sheep { + Sheep { name: name, naked: false } + } + + fn name(&self) -> String { + self.name.clone() + } + + fn noise(&self) -> String { + if self.is_naked() { + "baaaaah?".to_string() + } else { + "baaaaah!".to_string() + } + } + + // Default trait methods can be overridden. + fn talk(&self) { + // For example, we can add some quiet contemplation. + println!("{} pauses briefly... {}", self.name, self.noise()); + } +} + +fn main() { + // Type annotation is necessary in this case. + let mut dolly: Sheep = Animal::new("Dolly".to_string()); + // TODO ^ Try removing the type annotations. + + dolly.talk(); + dolly.shear(); + dolly.talk(); +} +``` fn main() { diff --git a/src/why-exercise.md b/src/why-exercise.md index b8f94a9..cf87a0f 100644 --- a/src/why-exercise.md +++ b/src/why-exercise.md @@ -9,6 +9,8 @@ This book was designed for easily diving into Rust,and it's very easy to use: - Difficulty from easy to super hard: easy 🌟 medium 🌟🌟 hard 🌟🌟🌟 super hard 🌟🌟🌟🌟 - Both [English](https://practice.rs) and [Chinsese](https://zh.practice.rs) are supported +> Part of our examples and exercises are borrowed from [Rust By Example](https://github.com/rust-lang/rust-by-example), thanks for the great works you have been doing! + ## Some of our exercises 🌟🌟🌟 Tuple struct looks similar to tuples, it has added meaning the struct name provides but has no named fields. It's useful when you want give the whole tuple a name, but don't care the fields's names.