Skip to content

RFC: moving out of enums #2329

Closed
Closed
@nikomatsakis

Description

@nikomatsakis

The existence of option::unwrap() (and soon reuslt::unwrap()) points at a more general problem. There is no way to move something out of an enum. I think we should be able to solve this more generally. My proposal is something like this:

alt move expr {
    foo(a, b, c) { ... }
}

In here, a, b, and c are not pointers into expr but rather the values themselves. They are moved out of expr, which must be something movable.

You could then write option::unwrap() like so:

fn unwrap<T>(opt: option<T>) {
    alt move opt { some(v) { v } none { /* ok */ } }
}

This fits best with adding move back as a unary operator (of which I am in favor). Basically, alt over an expression that is an explicit move causes the bound variables not to be region pointers into the struct but rather the values themselves (i.e., the types will differ). Depending how explicit we want to be, last use analysis is still needed in that last example to convert the use of v from a copy to a move.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions