为什么这种相互参考结构在Rust中起着特定的生命作用?
为什么以下代码编译?
我希望生锈的编译器告诉我
设置参考
(store.barber.set(some(& barber)))时,“借用的价值不够长”。
因为理发师的寿命比shop较短。
use core::cell::Cell;
struct Shop<'a> {
barber: Cell<Option<&'a Barber<'a>>>,
}
struct Barber<'a> {
shop: Cell<Option<&'a Shop<'a>>>,
}
fn main() {
let shop = Shop { barber: Cell::new(None) };
{
let barber = Barber { shop: Cell::new(Some(&shop))};
shop.barber.set(Some(&barber));
}
}
“ 在这里)。
( 注释/a3taps/newbie_how_can_i_reference_two_struct_in_each/“ rel =” nofollow noreferrer“>在这里。 但是,为什么整个事情都起作用,或者我的误解在哪里仍然不清楚。
进行编辑/启蒙
我在@netwaves答案上对代码
。如果链接不再起作用,也可以澄清这个问题。
内部范围只是为了使生活更加清晰。
用法看起来更像是这样:
use core::cell::Cell;
struct Shop<'a> {
barber: Cell<Option<&'a Barber<'a>>>,
shop_state: Cell<bool>,
}
impl<'a> Shop<'a> {
fn change_barber_state(&self) {
self.barber.get().unwrap().change_state();
}
fn change_state(&self) {
self.shop_state.set(!self.shop_state.get());
}
}
struct Barber<'a> {
shop: Cell<Option<&'a Shop<'a>>>,
barber_state: Cell<bool>,
}
impl<'a> Barber<'a> {
fn change_state(&self) {
self.barber_state.set(!self.barber_state.get());
}
fn change_shop_state(&self) {
self.shop.get().unwrap().change_state();
}
}
fn main() {
let shop = Shop {
barber: Cell::new(None),
shop_state: Cell::new(false),
};
let barber = Barber {
shop: Cell::new(Some(&shop)),
barber_state: Cell::new(false),
};
shop.barber.set(Some(&barber));
println!("{:?}", barber.barber_state);
println!("{:?}", shop.shop_state);
shop.change_barber_state();
barber.change_shop_state();
println!("{:?}", barber.barber_state);
println!("{:?}", shop.shop_state);
}
通过同一范围,寿命是一样的吗?
我认为一生也是通过释放资源来给出的,这些资源以相反的顺序与声明发生。或仅当 drop 特质是特征是实施的?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简短答案:
确实如此,因为根据您的代码,它实际上寿命足够长。
说明
问题(没问题)是您在内部范围之后不再使用
shop
,因此编译器足够聪明,可以说您的程序没有错。但是,如果您添加访问权限,他将开始抱怨,并且有充分的理由:
无法编译:
Playground> Playground
问题扩展:
显然,
shop
和barber
在同一程度上,直到完成后。在这种情况下,免费顺序无关紧要,因为编译器已经知道它们都不会再使用,因此上述代码是完全安全的。Short answer:
It does because as per your code it actually lives long enough.
Explanation
The problem (no problem) is that you are not using
shop
anymore after the inner scope, so the compiler is smart enough to say nothing is wrong with your program.But if you add an access he will start complaining, and with a good reason:
fails to compile with:
Playground
On the question extension:
Clearly both
shop
andbarber
life for the same extent, up to when main is finished. It doesn't matter the order of free in this case, since the compiler already know that none of them will be used anymore, so the above code is completely safe.