rust - 整个Box都可以 move 时,Box可以 move 其内容吗?

标签 rust heap-memory move-semantics borrowing

如果我Box::new一个值,请指向它的指针(借位检查器将不允许引用,因为我要 move 该框),然后 move Box,可以 move 值(例如重新分配)发生?
我以为Box仅存储值的地址,因此 move Box只会 move 地址。因此,是否有理由为什么借阅检查器在内容一成不变地借入时禁止 move 它?
Playground

最佳答案

不, move Box不会 move 堆中的值。Box保证:

a Box<T> is guaranteed to be represented as a single pointer


并且Rust保证 move 始终是按位复制(如果它们完全复制)。

Is there therefore a reason why borrow checker prohibits moving it when its contents are immutably borrowed?


这包括:
  • Is there any way to allow moving a container that has a borrowed element but not dropping it?
  • Why can't I store a value and a reference to that value in the same struct?
  • 关于rust - 整个Box都可以 move 时,Box可以 move 其内容吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64854198/

    相关文章:

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

    rust - 火箭.rs : Optional PathBuf has no matching routes

    multithreading - 如何检查线程是否已在 Rust 中完成?

    c - 结构数组的内存分配? 2个例子

    c++ - 将正在 move 的对象的成员作为参数传递是否安全

    math - 如何乘/除/加/减不同类型的数字?

    java - JVM 如何为对象分配内存,尽管它们以后可能会增长

    java - 如何处理客户端上需要大量内存的 Java 应用程序 ("-J-Xmx"?

    c++ - 为什么 Boost.Asio 处理程序必须是可复制构造的?

    rust - 为什么 Rust 不允许在一种类型上复制和删除特征?