user2665887
user2665887

Reputation: 923

Path statement leaves a value in moved-from state?

The surprisingly behaving code fragment:

let p = &mut 1i;
p; // note: `p` moved here because it has type `&mut int`, which is non-copyable
p; // error: use of moved value: `p`

Is this a bug or an intended behaviour?

Upvotes: 4

Views: 164

Answers (1)

brson
brson

Reputation: 1714

It's intended. &mut T is an owned type, so when you mention it as an expression it moves. You don't normally notice this because methods calls have their own reborrowing rules that give the callee a temporary mutable borrow of the self value (to avoid the annoyance of your mutable reference moving away).

Upvotes: 3

Related Questions