在我不再需要它之后如何忘记匹配中的引用?

时间:2017-08-12 20:54:01

标签: rust

在一场比赛中,我正在检查很多条件。如果检查成功,我想保存该项目。这是只有一个条件的简化代码:

struct Item {
    node: ItemKind,
}

struct Generics;

enum ItemKind {
    Impl(Generics),
}

fn main() {
    let item = Item { node: ItemKind::Impl(Generics) };
    let mut items = Vec::<Item>::new();
    match item {
        Item { node: ItemKind::Impl(ref generic), .. } => {
            // do some checks with generic
            // here we not need it anymore
            ::std::mem::forget(generic); // attempt to forget about generic doesn't help
            items.push(item);
        }
        _ => {}
    }
}

我收到了错误:

error[E0505]: cannot move out of `item` because it is borrowed
  --> src/main.rs:19:24
   |
15 |         Item { node: ItemKind::Impl(ref generic), .. } => {
   |                                     ----------- borrow of `item.node.0` occurs here
...
19 |             items.push(item);
   |                        ^^^^ move out of `item` occurs here

我不需要行generic上的items.push(item);引用,但我不知道如何告诉编译器。

0 个答案:

没有答案