约束
正如泛型类型可以被约束一样,生命周期(本身也是泛型)也可以使用约束。这里的 : 符号含义略有不同,但 + 的用法相同。请注意以下表达的含义:
T: 'a:T中的所有引用必须比生命周期'a存活更久。T: Trait + 'a:类型T必须实现 traitTrait,并且T中的所有引用必须比'a存活更久。
下面的例子展示了上述语法在 where 关键字之后的实际应用:
Press ← or → to navigate between chapters
Press S or / to search in the book
Press ? to show this help
Press Esc to hide this help