문제

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