collections - 有什么方法可以允许移动具有借入元素但不能删除它的容器?

标签 collections rust borrow-checker

我有这个容器:

use std::ptr::NonNull;

struct Container {
    data: NonNull<u8>,
}

impl Container {
    fn new() -> Container {
        todo!()
    }
    fn borrow_some_heap_data<'a>(&'a self) -> &'a u8 {
        todo!()
    }
    fn borrow_some_heap_data_mut<'a>(&'a mut self) -> &'a mut u8 {
        todo!()
    }
}

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

fn main() {
    let container = Container::new();
    let data = container.borrow_some_heap_data(); // or mut

    {
        let container = container; // move

        // This is safe because the move is a memcpy and the heap data doesn't change.
        println!("{}", *data);
    }

    // This is not safe because the container has been dropped
    // println!("{}", *data);
}
error[E0505]: cannot move out of `container` because it is borrowed
  --> src/main.rs:30:25
   |
27 |     let data = container.borrow_some_heap_data(); // or mut
   |                --------- borrow of `container` occurs here
...
30 |         let container = container; // move
   |                         ^^^^^^^^^ move out of `container` occurs here
...
33 |         println!("{}", *data);
   |                        ----- borrow later used here
即使有引用,移动容器也是安全的。但是,丢弃它是不安全的。有什么办法可以在Rust中表达这一点,允许移动,但不允许drop
直到删除该结构,data才会释放。

最佳答案

不,没有办法在安全的Rust中执行此操作;编译器不够智能/没有语法可以区分容器的生存期和元素的生存期。
Why can't I store a value and a reference to that value in the same struct?:

There is a special case where the lifetime tracking is overzealous: when you have something placed on the heap. This occurs when you use a Box<T>, for example. In this case, the structure that is moved contains a pointer into the heap. The pointed-at value will remain stable, but the address of the pointer itself will move. In practice, this doesn't matter, as you always follow the pointer.

关于collections - 有什么方法可以允许移动具有借入元素但不能删除它的容器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64841409/

相关文章:

rust - 将 Struct <'a>::method as a ` 传递给 <'a> Fn(&Foo<' a>)` 以在闭包中使用

rust - 证明struct字段的生命周期超过另一个生命周期

java - 如何从具有相同类别的对象的数组列表中提取元素?

rust - 如何在 Rust 中将整数转换为字节文字?

rust - 如何将 `futures::Stream` 包装到任何实现 `Write` 的东西中?

rust - 参数位置中的 dyn Trait 是什么意思?

rust - Rust LinkedList 中的借用检查器错误的原因是什么?

java - 调用 java 集合使用对象的接口(interface)而不是对象的类?

java - 如何获取HashMap中某个key出现的次数?

java - 如何使用多个排序标准对 ArrayList 进行排序?