Skip to content

Assigning a linearly typed value to _ does trigger use of moved value errors on subsequent usages #15151

Closed
@brendanzab

Description

@brendanzab

This code fails to compile:

fn main() {
    let x = vec![1i, 2, 3];
    let _x = x;
    println!("{}", x);
}

Error:

<anon>:4:20: 4:21 error: use of moved value: `x`
<anon>:4     println!("{}", x);
                            ^
note: in expansion of format_args!
<std macros>:2:23: 2:77 note: expansion site
<std macros>:1:1: 3:2 note: in expansion of println!
<anon>:4:5: 4:23 note: expansion site
<anon>:3:9: 3:11 note: `x` moved here because it has type `collections::vec::Vec<int>`, which is moved by default (use `ref` to override)
<anon>:3     let _x = x;
                 ^~

Where as this compiles with no error:

fn main() {
    let x = vec![1i, 2, 3];
    let _ = x;
    println!("{}", x);
}

Output:

[1, 2, 3]

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions