Skip to content

Instantly share code, notes, and snippets.

@tarcieri
Created October 31, 2016 02:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tarcieri/88de37bb355cedbd910e9d3251a96b36 to your computer and use it in GitHub Desktop.
Save tarcieri/88de37bb355cedbd910e9d3251a96b36 to your computer and use it in GitHub Desktop.
Generics, traits, and lifetimes, oh my!
pub trait MyTrait<'a> {
fn foo(n: &'a i32);
}
struct Derp;
impl<'a> MyTrait<'a> for Derp {
fn foo(n: &'a i32) {
}
}
struct Herp<'a, T> where T: MyTrait<'a> {
derp: T
}
fn main() {
Herp::<Derp>::foo(42);
}
@tarcieri
Copy link
Author

This is a rather contrived example, but I think it captures my current problem.

If I do struct Herp<T> I get:

error[E0261]: use of undeclared lifetime name `'a`
  --> example.rs:12:33
   |
12 | struct Herp<T> where T: MyTrait<'a> {
   |                                 ^^ undeclared lifetime

If I do struct Herp<'a, T> I get:

error[E0392]: parameter `'a` is never used
  --> example.rs:12:13
   |
12 | struct Herp<'a, T> where T: MyTrait<'a> {
   |             ^^ unused type parameter
   |
   = help: consider removing `'a` or using a marker such as `std::marker::PhantomData`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment