Rustc ignoring my lifetime parameter


#1
  --> src/population.rs:95:11
   |
95 |     m:&'a Mutator,
   |           ^^^^^^^ expected lifetime parameter

I can see a lifetime parameter, why can rustc not?

I realise I have not posted context but there is a lot of context. I am hoping that this is a common class of problem with a well known (not by me) procedure to debug it.

cargo explain E0106 does not help as it does not include the case where a life time parameter is both present and missing

If not I ill have to make a minimal case…


#2

Mutator itself has a lifetime parameter and you need to provide that.


#3

To clarify, maybe something like this: Mutator<'a>


#4

Ok. While I was waiting for your help (thank you) I ipped upa a minimal example. Was a lot easiert than I thought.

pub struct Mutator<'a> {
    names:Vec<String>,
}

struct Mod2<'a> {
    m3:&'a Mutator,
}

fn main() {
    
}

Is it a error to use the same life time parameter for both Mutator and Mod2?
If I give a different life time parameter to Mutator what is the syntax to use it to silence this error?

W


#5

Change:

struct Mod2<'a> {
    m3:&'a Mutator<'a>
}

results

6 |     m3:&'a Mutator<'a>
  |            ^^^^^^^^^^^ unexpected lifetime parameter

I have tried every other combination of life time parameters I can think of but to no avail.


#6
pub struct Mutator {
    names:Vec<String>,
}

struct Mod2<'a> {
    m3:&'a Mutator,
}

fn main() {
    
}

compiles. I am stumped. I know not why.


#7

What does the Mutator type definition look like in your real case?


#8

Owned types don’t need lifetime annotations:

pub struct Mutator {
    names:Vec<String>,
}

only temporary borrows need them, e.g.:

pub struct Mutator<'a> {
    names:Vec<&'a str>,
}

So Foo means it’s OK to use anywhere you want, but Foo<'a> means it’s tied to some temporary value stored elsewhere (in the place where 'a came from).