Skip to content

Implicit re-borrows of &mut references where a move would be expected #25899

Closed
@elinorbgr

Description

@elinorbgr

Consider this snippet of code:

fn main() {
    let a: &mut i32 = &mut 0;
    { let b = a; }
    let c = a;
}

It properly fails to compile, as a is moved into b.

However this one compiles fine:

fn main() {
    let a: &mut i32 = &mut 0;
    { let b: &mut i32 = a; }
    let c = a;
}

Adding a type annotation to b implicitly changed the move of a into a re-borrow. This behavior actually makes sense: if b was annotated &i32, a move would have not been possible, and a re-borrow would have been mandatory.

It is pretty harmless and does not pose any safety issue (actually I think having a "always re-borrow" policy would still be safe), but is still an implicit behavior that is not documented (at least I didn't find any documentation about it).

(Comes from a SO question: http://stackoverflow.com/q/30535529/2536143 )

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