update readme.md
This commit is contained in:
parent
029fdd1a24
commit
5463a95d4e
|
@ -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.
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
1.
|
||||
```rust
|
||||
```
|
||||
|
||||
1.
|
||||
```rust
|
||||
```
|
|
@ -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() {
|
||||
|
|
|
@ -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.
|
||||
|
|
Loading…
Reference in New Issue