Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Rust &引用;借来的价值活得不够长。”;实施Drop特征后_Rust - Fatal编程技术网

Rust &引用;借来的价值活得不够长。”;实施Drop特征后

Rust &引用;借来的价值活得不够长。”;实施Drop特征后,rust,Rust,作为rust的新手,我想处理一些数据结构,最终得到了类似于没有有效负载的节点类型的东西 use std::cell::RefCell; use std::collections::HashMap; use std::ops::Drop; #[derive(Debug)] struct Container<'a> { next : Option<&'a RefCell<Container<'a>>>, } impl<'a&g

作为rust的新手,我想处理一些数据结构,最终得到了类似于没有有效负载的节点类型的东西

use std::cell::RefCell;
use std::collections::HashMap;
use std::ops::Drop;

#[derive(Debug)]
struct Container<'a> {
    next : Option<&'a RefCell<Container<'a>>>,
}

impl<'a> Container<'a> {
    fn new() -> Container<'a> {
        Container { next: None }
    }

    fn set(&mut self, next: &'a RefCell<Container<'a>>) {
        self.next = Some(next);
    }
}
因为我在玩游戏,所以我尝试在容器类型上实现Drop特性

impl<'a> Drop for Container<'a>{
    fn drop(&mut self) {}
}
impl{
fn drop(&mut self){}
}
这将导致两个(另一个用于cont_2)

错误[E0597]:`cont_1`寿命不够长
-->src/main.rs:11:15
|
11 |设b|1=&cont|1;
|^^^^^^^^^借来的价值寿命不够长
...
18 | }
| -
| |
|‘cont_1’

|当删除“cont_1”并运行类型为“std::cell::RefCell”的析构函数时,这里可能会使用借用。如果在
drop()
实现中使用
self.next.unwrap()…
,会发生什么?由于您的一个变量必须在另一个变量之前被删除,所以最后一个变量将有一个悬空引用,因此行为未定义。因此,您的代码在“不编译”中是正确的

在我看来,解决方案是使用某种引用计数指针。如果您不想要
Rc
,因为他们不拥有邻居(这将创建一个引用循环,从而泄漏您的对象),您可以使用
引用。大概是这样的:

使用std::cell::RefCell;
使用std::ops::Drop;
使用std::rc::{rc,弱};
#[导出(调试)]
结构容器{
下一步:选择,
}
impl容器{
fn new()->容器{
容器{下一个:无}
}
fn集合(&mut self,next:&Rc){
self.next=Some(Rc::降级(next));
}
}
集装箱装卸{
fn drop(&mut self){}
}
fn main(){
//项目:
让cont_1=Rc::new(RefCell::new(Container::new());
让cont_2=Rc::new(RefCell::new(Container::new());
cont_1.借用mut().set(&cont_1);
cont_2.借用mut().set(&cont_2);
println!(“{:?}”,cont_1.borrow());
}

“目标是让这些节点不拥有它们的邻居,因此std::rc::rc是不可能的。”?“所以我做了一些测试,结果很好”定义“很好”,因为运行代码会破坏堆栈。关于std::rc::rc:我的理解是,通过调用.clone(),它会导致指针归多个实体所有。关于“罚款”:是的,承认这是非常宽松的。“罚款”可替换为“已编制”。
impl<'a> Drop for Container<'a>{
    fn drop(&mut self) {}
}
error[E0597]: `cont_1` does not live long enough
  --> src/main.rs:11:15
   |
11 |     let b_1 = &cont_1;
   |               ^^^^^^^ borrowed value does not live long enough
...
18 | }
   | -
   | |
   | `cont_1` dropped here while still borrowed
   | borrow might be used here, when `cont_1` is dropped and runs the destructor for type `std::cell::RefCell<Container<'_>>`
use std::cell::RefCell;
use std::ops::Drop;
use std::rc::{Rc, Weak};

#[derive(Debug)]
struct Container {
    next : Option<Weak<RefCell<Container>>>,
}

impl Container {
    fn new() -> Container {
        Container { next: None }
    }
    fn set(&mut self, next: &Rc<RefCell<Container>>) {
        self.next = Some(Rc::downgrade(next));
    }
}

impl Drop for Container{
    fn drop(&mut self) {}
}

fn main() {
    // items:
    let cont_1 = Rc::new(RefCell::new(Container::new()));
    let cont_2 = Rc::new(RefCell::new(Container::new()));

    cont_1.borrow_mut().set(&cont_1);
    cont_2.borrow_mut().set(&cont_2);

    println!("{:?}", cont_1.borrow());
}