小编Spe*_*nce的帖子

在解构一对盒子时附带移动错误

以下两行:

let x = Box::new(("slefj".to_string(), "a".to_string()));
let (a, b) = *x;
Run Code Online (Sandbox Code Playgroud)

产生错误:

error[E0382]: use of moved value: `x`
 --> src/main.rs:3:13
  |
3 |     let (a, b) = *x;
  |          -  ^ value used here after move
  |          |
  |          value moved here
  |
  = note: move occurs because `x.0` has type `std::string::String`, which does not implement the `Copy` trait
Run Code Online (Sandbox Code Playgroud)

有趣的是,如果我使用包含多个部分的枚举类型执行此操作,我会得到一个稍微不同的错误:

enum Tree {
    Nil,
    Pair(Box<Tree>, Box<Tree>),
}

fn main() {
    let x = Box::new(Tree::Nil);

    match *x {
        Tree::Pair(a, …
Run Code Online (Sandbox Code Playgroud)

rust

5
推荐指数
1
解决办法
447
查看次数

标签 统计

rust ×1