Skip to content

Proposed backward-compatible syntax for owning and output references #1780

Closed
@NXTangl

Description

@NXTangl

Since move is already a keyword, I propose the following:

&move T indicates a move reference to T. A move reference implicitly moves out of whatever it borrows, and dereferencing it consumes the reference, yielding a value of type T, but it may also be borrowed as &T and &mut T. &move T is NOT copy, and does not allow reborrowing like &mut T does. The borrowed variable is considered dropped at the end of the lifetime of the &move T by borrowck. Drop::drop() will be implicitly called.

&move mut T indicases an output reference to T. An output reference is considered an uninitialized variable by the borrowchecker. Only an uninitialized variable can be borrowed as &move mut, and it is considered initialized by borrowck after the end of the lifetime of the &move mut. Once initialized, it is considered identical to &mut T.

&move T is the owning reference that a lot of people have been wanting for a while, and &move mut T is my idea for a generalized placement new syntax, which might be necessary when/if we support unmovable types. An alternative to &move mut T would be a combination of placement syntax and guaranteed return-value optimization for functions returning unmovable types; I suppose the latter would be necessary anyway for them to work at all.

Thoughts? Reasons why this wouldn't actually parse? Alternative syntax?

Metadata

Metadata

Assignees

No one assigned

    Labels

    T-langRelevant to the language team, which will review and decide on the RFC.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions