如何在具有生命周期的特征上创建结构泛型?

时间:2018-06-14 12:19:46

标签: generics rust lifetime

我试图在需要一生的特性上创建结构Baz泛型:

trait Foo<'a> {
    fn foo(&self) -> &'a u8;
}

struct Baz<'a, T: Foo<'a>>(pub T);

不幸的是编译器认为'a中的Baz是无用的:

error[E0392]: parameter `'a` is never used
  --> src/main.rs:20:12
   |
20 | struct Baz<'a, T: Foo<'a>>(pub T);
   |            ^^ unused type parameter
   |
   = help: consider removing `'a` or using a marker such as `std::marker::PhantomData`

如何表达BazFoo的实现者之间的生命关系?

Reproduction on playground

0 个答案:

没有答案
相关问题