0

I'm trying to create structure Baz generic over a trait which requires a lifetime:

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

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

Unfortunately the compiler thinks that 'a in Baz is useless:

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`

How can I express the relation of lifetimes between Baz and the implementors of Foo?

Reproduction on playground

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
CodeSandwich
  • 1,601
  • 13
  • 23
  • I believe your question is answered by the answers of [“parameter `'a` is never used” error when 'a is used in type parameter bound](https://stackoverflow.com/questions/40484154/parameter-a-is-never-used-error-when-a-is-used-in-type-parameter-bound). If you disagree, please [edit] your question to explain the differences. Otherwise, we can mark this question as already answered. – Shepmaster Jun 14 '18 at 13:32
  • 1
    [DK's answer to the other question, applied to your situation](http://play.rust-lang.org/?gist=4b4d88770eadbff78597857dce6dfd7c&version=stable&mode=debug) and [my answer applied to your situation](http://play.rust-lang.org/?gist=991a6a9cea3eac71882870cf55b55532&version=stable&mode=debug). If you [use an associated type of the trait inside `Baz`](http://play.rust-lang.org/?gist=b8b7a0931666a8e4c16b240e1af7915c&version=stable&mode=debug) it will also work that way. – trent Jun 14 '18 at 13:55
  • Thank you @trentcl, that's the best solution for my real world use case! Also thank both of you for linking the older question, I couldn't find it. – CodeSandwich Jun 14 '18 at 20:30

0 Answers0