mirror of
https://github.com/sunface/rust-by-practice.git
synced 2025-06-23 12:39:42 +00:00
add zh/advanced-traits.md
This commit is contained in:
@ -17,7 +17,7 @@ Using of `Address` is much more clearable and convenient than `AsRef<[u8]> + Clo
|
|||||||
struct Container(i32, i32);
|
struct Container(i32, i32);
|
||||||
|
|
||||||
// USING associated types to re-implement trait Contains.
|
// USING associated types to re-implement trait Contains.
|
||||||
// trait Container {
|
// trait Contains {
|
||||||
// type A;
|
// type A;
|
||||||
// type B;
|
// type B;
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
# Advance Traits
|
# 进一步深入特征
|
||||||
|
|
||||||
## Associated types
|
## 关联类型
|
||||||
The use of "Associated types" improves the overall readability of code by moving inner types locally into a trait as output types. For example :
|
关联类型主要用于提升代码的可读性,例如以下代码 :
|
||||||
```rust
|
```rust
|
||||||
pub trait CacheableItem: Clone + Default + fmt::Debug + Decodable + Encodable {
|
pub trait CacheableItem: Clone + Default + fmt::Debug + Decodable + Encodable {
|
||||||
type Address: AsRef<[u8]> + Clone + fmt::Debug + Eq + Hash;
|
type Address: AsRef<[u8]> + Clone + fmt::Debug + Eq + Hash;
|
||||||
@ -9,15 +9,15 @@ pub trait CacheableItem: Clone + Default + fmt::Debug + Decodable + Encodable {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Using of `Address` is much more clearable and convenient than `AsRef<[u8]> + Clone + fmt::Debug + Eq + Hash`.
|
相比 `AsRef<[u8]> + Clone + fmt::Debug + Eq + Hash`, `Address` 的使用可以极大的极少其它类型在实现该特征时所需的模版代码.
|
||||||
|
|
||||||
1. 🌟🌟🌟
|
1. 🌟🌟🌟
|
||||||
```rust,editable
|
```rust,editable
|
||||||
|
|
||||||
struct Container(i32, i32);
|
struct Container(i32, i32);
|
||||||
|
|
||||||
// USING associated types to re-implement trait Contains.
|
// 使用关联类型实现重新实现以下特征
|
||||||
// trait Container {
|
// trait Contains {
|
||||||
// type A;
|
// type A;
|
||||||
// type B;
|
// type B;
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user