Update advance-trait.md

增加了为关联类型添加限制。看别人写的代码被绕晕了,还没绕出来
pull/1489/head
gtofish 3 months ago committed by GitHub
parent ff9e90d15d
commit 297cc2cce9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -81,6 +81,15 @@ trait Container{
fn difference<C: Container>(container: &C) {} fn difference<C: Container>(container: &C) {}
``` ```
**关联类型可以增加其他特征限制**
例如限定类型必须实现了core::fmt::Display trait:
```rust
trait Container{
type ADisplay;
type B;
fn contains(&self, a: &Self::A, b: &Self::B) -> bool;
}
很多库里面都有这样的写法,大家别被绕晕
## 默认泛型类型参数 ## 默认泛型类型参数

Loading…
Cancel
Save