rust - 在 Vec 中嵌套生命周期时,生命周期参数的数量错误

标签 rust

我正在尝试构建一个包含对另一个结构的向量的引用的结构,如下所示:

pub struct Downstream<'a> {
    frequency: i32,
    slot: i32,
    connector: i32,
    description: String,
    cablemac: &'a CableMac,
}

pub struct Upstream<'a> {
    downstreams: Vec<Downstream>,
}

无论我尝试什么,我总是会遇到这个生命周期错误:

src/e6000/mod.rs:13:22: 13:32 error: wrong number of lifetime parameters: expected 1, found 0 [E0107]
src/e6000/mod.rs:13     downstreams: Vec<Downstream>,

E0107 一点用都没有。

我在哪里以及如何放置一个 'a 来让它工作?

最佳答案

downstreams: Vec<Downstream>,

应该是

downstreams: Vec<Downstream<'a>>,

E0107 doesn't help at all.

您应该运行 rustc --explain E0107

该命令的输出当前以一些很好的示例开头:

This error means that an incorrect number of lifetime parameters were provided for a type (like a struct or enum) or trait.

Some basic examples include:

struct Foo<'a>(&'a str);
enum Bar { A, B, C }

struct Baz<'a> {
    foo: Foo,     // error: expected 1, found 0
    bar: Bar<'a>, // error: expected 0, found 1
}

关于rust - 在 Vec 中嵌套生命周期时,生命周期参数的数量错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37158911/

相关文章:

pointers - 将实现特征的数据存储在向量中

rust - 使用 Rust 稳定版和夜间 channel 允许并行编译代码有多难?

rust - 如何使用构建器模式生成的数据

rust - 是否有相当于python中的 `any`方法

generics - 如何将 ndarray 中的元素与 Rust 中的泛型相乘?

rust - 我如何使用 Rust 将匹配的值分配为结果?

bit-manipulation - 将负符号添加到无符号的更快方法

operator-overloading - 如何为不同的 RHS 类型和返回值重载运算符?

rust - `assert_eq!(a, b)` 和 `assert_eq!(a, b, )` 之间有区别吗?

parallel-processing - 如何使用 Rayon 检测整数总和的溢出?