Skip to content

can't move out nested boxes #16227

Closed
Closed
@goffrie

Description

@goffrie

The following code seems like it should work, but it produces a cryptic error:

fn main() {
    let f = box Some((box 1i, box 2i));
    let g = match f { box Some((a, b)) => (*b, *a), _ => (0,0) };
    println!("{}", g);
}
<anon>:3:36: 3:37 error: use of partially moved value: `f#0#1`
<anon>:3     let g = match f { box Some((a, b)) => (*b, *a), _ => (0,0) };
                                            ^
<anon>:3:33: 3:34 note: `f#0#0` moved here because it has type `Box<int>`, which is moved by default (use `ref` to override)
<anon>:3     let g = match f { box Some((a, b)) => (*b, *a), _ => (0,0) };
                                         ^
error: aborting due to previous error

However, removing the outermost box works:

fn main() {
    let f = Some((box 1i, box 2i));
    let g = match f { Some((a, b)) => (*b, *a), _ => (0,0) };
    println!("{}", g); // => (2, 1)
}

Alternatively, matching on an rvalue also works:

fn main() {
    let f = box Some((box 1i, box 2i));
    let g = match f.clone() { box Some((a, b)) => (*b, *a), _ => (0,0) };
    println!("{}", g); // => (2, 1)
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions