From 297cc2cce908cf9015553b04fc8ca05fb77f61bd Mon Sep 17 00:00:00 2001 From: gtofish <106068223+gtofish@users.noreply.github.com> Date: Thu, 5 Dec 2024 23:33:58 +0800 Subject: [PATCH] Update advance-trait.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加了为关联类型添加限制。看别人写的代码被绕晕了,还没绕出来 --- src/basic/trait/advance-trait.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/basic/trait/advance-trait.md b/src/basic/trait/advance-trait.md index 16a574ed7..d6103853a 100644 --- a/src/basic/trait/advance-trait.md +++ b/src/basic/trait/advance-trait.md @@ -81,6 +81,15 @@ trait Container{ fn difference(container: &C) {} ``` +**关联类型可以增加其他特征限制** +例如:限定类型必须实现了core::fmt::Display trait: +```rust +trait Container{ + type A:Display; + type B; + fn contains(&self, a: &Self::A, b: &Self::B) -> bool; +} +很多库里面都有这样的写法,大家别被绕晕 ## 默认泛型类型参数