Skip to content

RFC: Allow copyless destructuring of tuples #2517

Closed
@erickt

Description

@erickt

Destructuring a tuple now warns on an implicit copy. For example, this code:

fn main() {
    let x = [("a", "b")];
    for x.each { |ab|
        let (a, b) = ab;
        #error("%? : %?", a, b);
    }
}

Warns with this message:

test.rs:4:21: 4:23 warning: implicitly copying a non-implicitly-copyable value
test.rs:4         let (a, b) = ab;

To fix it, we need to change the destructure to let (a, b) = copy ab;. The copy isn't needed for situations like this though. This code could be replaced with an alt-block to avoid the copy:

fn main() {
    let x = [("a", "b")];
    for x.each { |ab|
        alt ab {
          (a, b) { #error("%? : %?", a, b) }
        }
    }
}

But then that just adds more indention creep. Ideally rust would infer the usage and not require a copy, but if that's not possible, nmatsakis mentioned we may be able to create region pointers when destructuring. He suggested a syntax like let (&a, &b) = ab;. This syntax could then also be used in alt-blocks to keep the destructuring syntax consistent.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-codegenArea: Code generation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions