作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我尝试返回对结构字段的可变引用。
pub trait Objective {
fn get_children<'a>(&'a mut self) -> &'_ mut Vec<&'_ mut Box<dyn Objective>>;
fn get_parent(&'_ mut self) -> &'_ mut Box<dyn Objective>;
fn update(&'_ self) -> ();
}
// #[derive(Objective)]
pub struct Object<'a> {
children: Vec<&'a mut Box<dyn Objective>>,
parent: &'a mut Box<dyn Objective>,
}
impl<'a> Objective for Object<'a> {
fn get_children(&'a mut self) -> &'_ mut Vec<&'_ mut Box<dyn Objective>> {
return &mut self.children;
}
fn get_parent(&'_ mut self) -> &'_ mut Box<dyn Objective> {
return self.parent;
}
fn update(&'_ self) -> () {}
}
生命周期有问题。这是编译器给出的错误:
error[E0308]: method not compatible with trait
--> src/lib.rs:14:5
|
14 | fn get_children(&'a mut self) -> &'_ mut Vec<&'_ mut Box<dyn Objective>> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ lifetime mismatch
|
= note: expected fn pointer `fn(&'a mut Object<'a>) -> &'a mut std::vec::Vec<&'a mut std::boxed::Box<(dyn Objective + 'static)>>`
found fn pointer `fn(&'a mut Object<'a>) -> &'a mut std::vec::Vec<&'a mut std::boxed::Box<(dyn Objective + 'static)>>`
note: the lifetime `'a` as defined on the method body at 14:5...
--> src/lib.rs:14:5
|
14 | fn get_children(&'a mut self) -> &'_ mut Vec<&'_ mut Box<dyn Objective>> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: ...does not necessarily outlive the lifetime `'a` as defined on the impl at 13:6
--> src/lib.rs:13:6
|
13 | impl<'a> Objective for Object<'a> {
| ^^
error[E0308]: method not compatible with trait
--> src/lib.rs:14:5
|
14 | fn get_children(&'a mut self) -> &'_ mut Vec<&'_ mut Box<dyn Objective>> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ lifetime mismatch
|
= note: expected fn pointer `fn(&'a mut Object<'a>) -> &'a mut std::vec::Vec<&'a mut std::boxed::Box<(dyn Objective + 'static)>>`
found fn pointer `fn(&'a mut Object<'a>) -> &'a mut std::vec::Vec<&'a mut std::boxed::Box<(dyn Objective + 'static)>>`
note: the lifetime `'a` as defined on the impl at 13:6...
--> src/lib.rs:13:6
|
13 | impl<'a> Objective for Object<'a> {
| ^^
note: ...does not necessarily outlive the lifetime `'a` as defined on the method body at 14:5
--> src/lib.rs:14:5
|
14 | fn get_children(&'a mut self) -> &'_ mut Vec<&'_ mut Box<dyn Objective>> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
这是我之前的尝试,但我没有明确指定生命周期:
pub trait Objective {
fn get_children(&'_ mut self) -> &'_ mut Vec<&'_ mut Box<dyn Objective>>;
fn get_parent(&'_ mut self) -> &'_ mut Box<dyn Objective>;
fn update(&'_ self) -> ();
}
// #[derive(Objective)]
pub struct Object<'a> {
children: Vec<&'a mut Box<dyn Objective>>,
parent: &'a mut Box<dyn Objective>,
}
impl Objective for Object<'_> {
fn get_children(&'_ mut self) -> &'_ mut Vec<&'_ mut Box<dyn Objective>> {
return &mut self.children;
}
fn get_parent(&'_ mut self) -> &'_ mut Box<dyn Objective> {
return self.parent;
}
fn update(&'_ self) -> () {}
}
我也遇到了类似的错误:
error[E0308]: mismatched types
--> src/lib.rs:15:16
|
15 | return &mut self.children;
| ^^^^^^^^^^^^^^^^^^ lifetime mismatch
|
= note: expected mutable reference `&mut std::vec::Vec<&mut std::boxed::Box<(dyn Objective + 'static)>>`
found mutable reference `&mut std::vec::Vec<&mut std::boxed::Box<(dyn Objective + 'static)>>`
note: the anonymous lifetime #1 defined on the method body at 14:5...
--> src/lib.rs:14:5
|
14 | / fn get_children(&'_ mut self) -> &'_ mut Vec<&'_ mut Box<dyn Objective>> {
15 | | return &mut self.children;
16 | | }
| |_____^
note: ...does not necessarily outlive the lifetime `'_` as defined on the impl at 13:27
--> src/lib.rs:13:27
|
13 | impl Objective for Object<'_> {
| ^^
我该如何解决这个问题?
最佳答案
在特征上指定生命周期并且它有效:
pub trait Objective<'a> {
fn get_children(&mut self) -> &mut Vec<&'a mut Box<dyn Objective<'a>>>;
fn get_parent(&mut self) -> &mut Box<dyn Objective<'a>>;
fn update(&self) -> ();
}
// #[derive(Objective)]
pub struct Object<'a> {
children: Vec<&'a mut Box<dyn Objective<'a>>>,
parent: Box<dyn Objective<'a>>,
}
impl<'a> Objective<'a> for Object<'a> {
fn get_children(&mut self) -> &mut Vec<&'a mut Box<dyn Objective<'a>>> {
&mut self.children
}
fn get_parent(&'_ mut self) -> &mut Box<dyn Objective<'a>> {
&mut self.parent
}
fn update(&'_ self) -> () {}
}
通过在 Objective
上指定生命周期,我们可以确保实现者将返回可变引用的 Vec
,满足与特征所需生命周期相同的生命周期要求.
关于rust - 从 trait 方法返回对结构字段的可变引用时,如何修复生命周期不匹配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63005820/
我正在开发一个使用多个 turtle 的滚动游戏。玩家 turtle 根据按键命令在 Y 轴上移动。当危害和好处在 X 轴上移动时,然后循环并改变 Y 轴位置。我尝试定义一个名为 colliding(
我不明白为什么他们不接受这个作为解决方案,他们说这是一个错误的答案:- #include int main(void) { int val=0; printf("Input:- \n
我正在使用基于表单的身份验证。 我有一个注销链接,如下所示: 以及对应的注销方法: public String logout() { FacesContext.getCurren
在 IIS7 应用程序池中有一个设置 Idle-time out 默认是 20 分钟,其中说: Amount of time(in minutes) a worker process will rem
我是一名优秀的程序员,十分优秀!