2

我有点难过。我希望通过以下代码。

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

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

fn main() {

} 

我从枚举开始,但是因为我需要使用&指针,所以我不得不给它一个生命周期。当我将该枚举添加到另一个结构时出现问题。我知道我不能给 struct 其他生命周期,'self但它给了我一个生命周期的错误,说 Source 中不允许匿名生命周期(这就像我没有添加任何生命周期一样)。

4

1 回答 1

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

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

fn main() {

} 

您使用生命周期参数声明了枚举 Source enum Source<'self>,. 这意味着您还必须在 Matrix 中声明 Source 的生命周期。

于 2013-08-29T00:03:28.883 回答