在多次阅读Rust书后,我认为我开始理解生命周期的要点了,但对我而言,另一个问题是我们需要使用的语法来声明它们。我发现这真的很不直观。
我已经将我的一些简单的代码简化为这两个结构体(其中一个引用另一个)。
如果您将此隔离的代码复制并粘贴到 main.rs 文件中并进行编译,则会出现以下错误:
如果我不注释矢量参数的生命周期,我会得到期望的: "检查你的生命周期, 伙计"(这很有道理)。
当然,如果我选择一个更激烈的解决方案,从我的结构中删除“vector”属性,因为所有其他属性都是标量,代码就可以编译。但我需要我的结构具有某种非标量数据结构。我怀疑我需要在Default初始化器中对我的向量进行某种寿命标记,但我不确定是什么。
顺便说一下,我认为我的TestStructB也已经正确地注释了生命周期,但可能不是。它看起来正确吗?
最后,我之所以到这里,是因为编译器说我需要为原始结构的引用(&)版本声明一个默认初始化器。这个原始结构已经在我的程序中快乐地使用了,但从来没有被引用过。每当我开始以&引用方式使用它时,Rust都声称它需要其默认初始化器。问题是,我仍然认为在使用我的引用结构时做错了什么,并且有一种使用&引用(和生命周期注释)的结构的正确方法,而不会让Rust抱怨它们(不存在的)默认初始化器。
编辑:@JohnKugelman是正确的,我通过与Rust编译器的典型新手斗争来遇到这个“异常情况”,试图克服它产生的主要是晦涩难懂的消息。
我的简化原始内容如下:
我已经将我的一些简单的代码简化为这两个结构体(其中一个引用另一个)。
#[derive(Debug, Default)]
pub struct TestStructA {
pub byte_1: u8,
pub byte_2: u8,
pub vector: Vec<u8>,
}
impl<'a> Default for &'a TestStructA {
fn default() -> &'a TestStructA {
&TestStructA { byte_1: 10, byte_2: 20, vector: 'a vec![1, 2, 3] }
}
}
#[derive(Debug, Default)]
pub struct TestStructB<'a> {
pub test_array: &'a [u8],
pub t_a: &'a TestStructA,
}
如果您将此隔离的代码复制并粘贴到 main.rs 文件中并进行编译,则会出现以下错误:
error: expected `while`, `for`, `loop` or `{` after a label
--> src/main.rs:10:59
|
10 | &TestStructA { byte_1: 10, byte_2: 20, vector: 'a vec![1, 2, 3] }
| ^^^ expected `while`, `for`, `loop` or `{` after a label
error: labeled expression must be followed by `:`
--> src/main.rs:10:59
|
10 | &TestStructA { byte_1: 10, byte_2: 20, vector: 'a vec![1, 2, 3] }
| ---^^^^^^^^^^^^^
| | |
| | help: add `:` after the label
| the label
|
= note: labels are used before loops and blocks, allowing e.g., `break 'label` to them
如果我不注释矢量参数的生命周期,我会得到期望的: "检查你的生命周期, 伙计"(这很有道理)。
error[E0515]: cannot return reference to temporary value
--> src/main.rs:10:9
|
10 | &TestStructA { byte_1: 10, byte_2: 20, vector: vec![1, 2, 3] }
| ^-------------------------------------------------------------
| ||
| |temporary value created here
| returns a reference to data owned by the current function
当然,如果我选择一个更激烈的解决方案,从我的结构中删除“vector”属性,因为所有其他属性都是标量,代码就可以编译。但我需要我的结构具有某种非标量数据结构。我怀疑我需要在Default初始化器中对我的向量进行某种寿命标记,但我不确定是什么。
顺便说一下,我认为我的TestStructB也已经正确地注释了生命周期,但可能不是。它看起来正确吗?
最后,我之所以到这里,是因为编译器说我需要为原始结构的引用(&)版本声明一个默认初始化器。这个原始结构已经在我的程序中快乐地使用了,但从来没有被引用过。每当我开始以&引用方式使用它时,Rust都声称它需要其默认初始化器。问题是,我仍然认为在使用我的引用结构时做错了什么,并且有一种使用&引用(和生命周期注释)的结构的正确方法,而不会让Rust抱怨它们(不存在的)默认初始化器。
编辑:@JohnKugelman是正确的,我通过与Rust编译器的典型新手斗争来遇到这个“异常情况”,试图克服它产生的主要是晦涩难懂的消息。
我的简化原始内容如下:
#[derive(Debug, Default)]
pub struct TestStructA {
pub byte_1: u8,
pub byte_2: u8,
pub vector: Vec<u8>,
}
#[derive(Debug, Default)]
pub struct TestStructB<'a> {
pub test_array: &'a [u8],
pub t_a: &'a TestStructA,
}
使用这段代码,我得到的错误是:
error[E0277]: the trait bound `&TestStructA: Default` is not satisfied
--> src/main.rs:11:5
|
11 | pub t_a: &'a TestStructA,
| ^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Default` is not implemented for `&TestStructA`
|
= help: the following implementations were found:
<TestStructA as Default>
= note: required by `std::default::Default::default`
= note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)
显然,rustc误导我认为我需要&Struct
默认初始化器?不能说...
impl<'a> Default for &'a TestStructA
-- 你是如何想到这个的?让我们往回追溯一下,找出你认为需要它的原因,因为这看起来非常可疑。我认为你应该删除整个代码块,然后询问我们导致你到达这里的原始错误。 - John Kugelmantest_array: Vec<u8>, t_a: TestStructA
。 - John KugelmanTestStructB
上使用Default
吗?你能把它移除吗? - John Kugelman