Question

I'm a bit stumped. I want following code to pass.

enum Source<'self> {
    String(&'self str),
    ReaderUtil(&'self ReaderUtil)
}

pub struct Matrix<'self> {
      source: &'self Source
}

fn main() {

} 

I started with just enum, but then because I needed to use &pointers, I had to give it a lifetime. Problem appeared when I added that enum to another struct. I know I can't give struct other lifetime than 'self but it gives me an error for lifetime, saying anonymous lifetimes aren't allowed in Source (which is same like I didn't add any lifetimes).

Was it helpful?

Solution

enum Source<'self> {
    String(&'self str),
    ReaderUtil(&'self ReaderUtil)
}

pub struct Matrix<'self> {
      source: &'self Source<'self>
}

fn main() {

} 

You declared your enum Source with a lifetime parameter, enum Source<'self>. That means you also have to declare the lifetime for Source in Matrix.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top