gpt4 book ai didi

rust - 该死的,如果你这样做,该死的,如果你不 : Rust compiler complains regardless whether there's a lifetime parameter or not

转载 作者:行者123 更新时间:2023-12-04 02:26:17 24 4
gpt4 key购买 nike

我正在尝试决定是否应该向我的 impls 添加生命周期参数,但似乎我处于“如果你这样做该死,如果你不这样做该死”的情况,因为无论是否有生命周期,编译器都会提示参数与否。

pub struct TurtleRef<'a> {
t: &'a BorrowedTurtle<'a>,
}

impl TurtleRef<'_> {
pub fn borrowed_turtle(&self) -> BorrowedTurtle {
*self.t
}

pub fn new(r: Turtle) -> TurtleRef {
TurtleRef{t: &BorrowedTurtle{ t:r}}
}
}

pub struct BorrowedTurtle<'a> {
t: Turtle<'a>,
}

impl<'a> std::ops::Deref for BorrowedTurtle<'_> {
type Target = Turtle<'a>;

fn deref(&self) -> &Self::Target {
&self.t
}
}

impl<'a> std::ops::DerefMut for BorrowedTurtle<'_> {
type Target = Turtle<'a>;
fn deref_mut(&mut self) -> &mut Self::Target {
&self.t
}
}

pub struct Turtle<'a> {
children: Vec<Turtle<'a>>,
}

Turtle 有更多字段,但为了简单起见,我删除了它们。可以看到代码片段here .代码抛出错误

error[E0207]: the lifetime parameter `'a` is not constrained by the impl trait, self type, or predicates
--> src/campus.rs:54:6
|
54 | impl<'a> std::ops::Deref for BorrowedTurtle<'_> {
| ^^ unconstrained lifetime parameter

没问题,我将删除该参数,因为它引起了如此大惊小怪。但是在删除它之后,我得到了一大堆新错误:


error[E0261]: use of undeclared lifetime name `'a`
--> src/campus.rs:55:26
|
55 | type Target = Turtle<'a>;
| ^^ undeclared lifetime
|
help: consider introducing lifetime `'a` here
|
54 | impl<'a> std::ops::Deref for BorrowedTurtle<'_> {
| ^^^^
help: consider introducing lifetime `'a` here
|
55 | type Target<'a> = Turtle<'a>;
| ^^^^

无论您说什么,我都会继续并将该参数添加到目标。但是现在我又遇到了另一个错误:

error[E0658]: generic associated types are unstable
--> src/campus.rs:55:5
|
55 | type Target<'a> = Turtle<'a>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: see issue #44265 <https://github.com/rust-lang/rust/issues/44265> for more information

所以无论我做什么,似乎我都遇到了一个重大错误。如何在不从头开始的情况下停止这些错误?我想保留 impls、structs 和 deref 函数,所以我唯一应该改变的是它们的实现。

另一方面,我得到了错误

error[E0437]: type `Target` is not a member of trait `std::ops::DerefMut`
--> src/campus.rs:64:5
|
64 | type Target = Turtle<'a>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^ not a member of trait `std::ops::DerefMut`

因为 Turtle 没有实现 DerefMut,事实上 Turtle 不应该实现 DerefMut。是否对 Turtle 进行了轻微修改,从而导致某些东西已经实现了 DerefMut?

最佳答案

这里有几个问题。首先:

error[E0207]: the lifetime parameter `'a` is not constrained by the impl trait, self type, or predicates
--> src/campus.rs:54:6
|
54 | impl<'a> std::ops::Deref for BorrowedTurtle<'_> {
| ^^ unconstrained lifetime parameter

您要么使用匿名生命周期,要么不使用。在这里,您声明了 'a,所以使用它:

impl<'a> std::ops::Deref for BorrowedTurtle<'a> {

要使用省略的生命周期,您不必声明它:

impl std::ops::Deref for BorrowedTurtle<'_> {

但是这里必须引用Target中的生命周期,不能省略。

第二个:

error[E0437]: type `Target` is not a member of trait `std::ops::DerefMut`
--> src/lib.rs:28:5
|
28 | type Target = Turtle<'a>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^ not a member of trait `std::ops::DerefMut`

DerefMut 没有 Target 成员,因为它重用了其超特性 Deref 中的成员。这是为了确保项目必须 DerefDerefMut 到相同的 Target:

impl<'a> std::ops::Deref for BorrowedTurtle<'a> {
type Target = Turtle<'a>;

fn deref(&self) -> &Self::Target {
&self.t
}
}

impl std::ops::DerefMut for BorrowedTurtle<'_> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.t
}
}

最后,您现在会收到未使用 'a 的错误:

error[E0392]: parameter `'a` is never used
--> src/lib.rs:15:27
|
15 | pub struct BorrowedTurtle<'a> {
| ^^ unused parameter
|
= help: consider removing `'a`, referring to it in a field, or using a marker such as `PhantomData`

这是因为您有几个递归类型,其中实际上没有使用生命周期:

// 'a isn't actually used for anything

pub struct Turtle<'a> {
children: Vec<Turtle<'a>>,
}

pub struct BorrowedTurtle<'a> {
t: Turtle<'a>,
}

我假设您为了这个答案省略了其他使用 'a 的相关字段,仅此而已!

关于rust - 该死的,如果你这样做,该死的,如果你不 : Rust compiler complains regardless whether there's a lifetime parameter or not,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67395001/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com