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).

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top