代码:
use std::collections::HashSet;
use std::{mem, ptr, fmt};
use std::ops::Deref;
enum Unsafety {
Normal
}
enum ImplPolarity { Positive }
struct TraitRef;
struct Ty;
struct ImplItem;
enum ItemKind {
Impl(Unsafety,
ImplPolarity,
Option<TraitRef>, // (optional) trait this impl implements
Box<Ty>, // self
),
}
struct Item {
node: ItemKind,
}
pub struct P<T: ?Sized> {
ptr: Box<T>
}
impl<T: 'static> P<T> {
pub fn unwrap(self) -> T {
*self.ptr
}
}
impl<T: ?Sized> Deref for P<T> {
type Target = T;
fn deref(&self) -> &T {
&self.ptr
}
}
fn main() {
let mut items = Vec::<P<Item>>::new();
let mut item2: Item;
for item in items.drain(..) {
if let ItemKind::Impl(Unsafety::Normal,
ImplPolarity::Positive,
Some(ref trait_type),
ref for_type) = item.node {
} else {
// item2 = *item; // AAA
item2 = item.unwrap(); // BBB
}
}
}
产生编译时错误:
error[E0505]: cannot move out of `item` because it is borrowed
--> /home/xxx/.emacs.d/rust-playground/at-2017-07-29-204629/snippet.rs:64:21
|
61 | ref for_type) = item.node {
| ---- borrow of `item` occurs here
...
64 | item2 = item.unwrap();
我不明白两件事:
为什么它会提示
if
中的借用我们在else
时分支分支?它们应该是相互排斥的,其中一个的借用不应影响另一个。如果我替换
Vec
在let mut items = Vec::<P<Item>>::new();
与Vec<Box<Item>>
并取消注释行AAA
和评论行BBB
,然后编译。两者Box
和P
实现Deref
, 所以item.node
表达式应该相同。
最佳答案
这是一个更清晰的例子:
struct Item;
struct P<T> {
ptr: Box<T>,
}
impl<T> P<T> {
fn new(v: T) -> Self {
P { ptr: Box::new(v) }
}
}
impl<T> std::ops::Deref for P<T> {
type Target = T;
fn deref(&self) -> &T {
&self.ptr
}
}
fn main() {
let mut item = P::new(Item);
// let mut item = Box::new(Item);
*item;
}
Both
Box
andP
implementDeref
, so theitem.node
expression should be the same.
硬道理时间:移出 Box
是 special-cased in the compiler .它不使用Deref
。移出 Box
会释放内存并为您提供所有权。我们自己无法实现这种特殊能力。
也许在未来的某个时候,将添加假设的特征,如DerefMove
。这种特质很难得到正确的。有一个number of attempts for an RFC for it , 但目前都没有开放。
另见:
关于rust - 为什么我得到 "cannot move out of ` item` 因为它是为自定义类型而不是 Box 借来的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45392688/