Rust 我如何防止在仍然被借用的情况下将impl Trait移到函数中而被丢弃?

Rust 我如何防止在仍然被借用的情况下将impl Trait移到函数中而被丢弃?,rust,lifetime,Rust,Lifetime,我想定义一个traitHolder,它可以通过items方法迭代一些内容。我不想从这个方法返回trait对象,因为如果可能的话,我希望使用静态分派和堆栈分配。我拥有的系统运行良好,但在某种令人惊讶的情况下失败了 代码如下: pub-trait-Holder{ 类型项:迭代器Self::Items; } 结构Impl{ 项目:Vec } impl持有人; fn项目(&'a self)->self::项目{ self.items.iter() } } fn使用支架(支架:impl支架){ -----

我想定义一个trait
Holder
,它可以通过
items
方法迭代一些内容。我不想从这个方法返回trait对象,因为如果可能的话,我希望使用静态分派和堆栈分配。我拥有的系统运行良好,但在某种令人惊讶的情况下失败了

代码如下:

pub-trait-Holder{
类型项:迭代器Self::Items;
}
结构Impl{
项目:Vec
}
impl持有人;
fn项目(&'a self)->self::项目{
self.items.iter()
}
}
fn使用支架(支架:impl支架){
------------------帮助:考虑添加一个显式的生命界……:“IMPL持有者”可能活得不够长。
-->src/graph/test.rs:20:24
|
19 | fn使用|支架(支架:impl支架+a`
20 |用于支架中的项目。项目(){
|                        ^^^^^
|
注意:…以便引用类型`&'a impl Holder Self::Items;
}
结构Impl{
项目:Vec
}
impl持有人;
fn项目(&'a self)->self::项目{
self.items.iter()
}
}
fn使用固定器(固定器:impl holder src/graph/test.rs:20:17
|

19 | fn use_holder(holder:impl holder这实际上是非常困难的-使用寿命和相关类型似乎是锈迹中(尚未)得到很好支持的奇怪的角落案例之一。如果我说我正确理解了错误的原因,那我是在撒谎,但是我已经通过使用

您只需更改一行:

fn使用固定器(固定器:impl for){
请注意,对于每个可能的寿命
'a
,寿命参数已移动到
,而不是对于至少与函数寿命相同的特定
'a


你需要使用
,因为如果没有具体的用例,很难知道你想要做什么,但是trait的生命周期有一些限制,关于这个明确的答案,请参见GATsThanks。它可以编译和运行,但似乎不符合要求。正如链接中所描述的,我还能够获得where版本。也许有一天,你会发现你可以用《服务贸易总协定》来表达这一点,但它仍然是不完整的,可能会改变。
error[E0309]: the parameter type `impl Holder<'a, N>` may not live long enough
  --> src/graph/test.rs:20:17
   |
19 | fn use_holder<'a, N: 'a>(holder: impl Holder<'a, N>) {
   |                                  ------------------ help: consider adding an explicit lifetime bound...: `impl Holder<'a, N> + 'a`
20 |     for item in holder.items() {
   |                 ^^^^^^
   |
note: ...so that the type `impl Holder<'a, N>` is not borrowed for too long
  --> src/graph/test.rs:20:17
   |
20 |     for item in holder.items() {
   |                 ^^^^^^

error[E0309]: the parameter type `impl Holder<'a, N>` may not live long enough
  --> src/graph/test.rs:20:24
   |
19 | fn use_holder<'a, N: 'a>(holder: impl Holder<'a, N>) {
   |                                  ------------------ help: consider adding an explicit lifetime bound...: `impl Holder<'a, N> + 'a`
20 |     for item in holder.items() {
   |                        ^^^^^
   |
note: ...so that the reference type `&'a impl Holder<'a, N>` does not outlive the data it points at
  --> src/graph/test.rs:20:24
   |
20 |     for item in holder.items() {
   |                        ^^^^^

error: aborting due to 2 previous errors; 4 warnings emitted

For more information about this error, try `rustc --explain E0309`.
error[E0597]: `holder` does not live long enough
  --> src/graph/test.rs:20:17
   |
19 | fn use_holder<'a, N: 'a>(holder: impl Holder<'a, N> + 'a) {
   |               -- lifetime `'a` defined here
20 |     for item in holder.items() {
   |                 ^^^^^^--------
   |                 |
   |                 borrowed value does not live long enough
   |                 argument requires that `holder` is borrowed for `'a`
...
23 | }
   | - `holder` dropped here while still borrowed