For function call, does compiler treat move as copy for '&mut T' type?

As we all know, any type that not impl Copy trait will move.

Below assignment statement is move:

{
	let mut i = 0;
	let j = &mut i; // `&mut i32` does not impl Copy trait
	let x = j;
	let y = j; // OK, use of moved value: `j`
}

Below function call is move:

{
	struct I {}
	fn consume(t: I) {}

	let t: I = I {};
	consume(t); // `I` does not impl Copy trait, so `t` is moved here.
	t; // OK: we can not use `t` now
}

Below function call seems copy:

{
	fn consume(t: &mut i32) {}

	let mut i: i32 = 0;
	let t: &mut i32 = &mut i;
	consume(t); // `&mut i32` does not impl Copy trait, so `t` is moved here.
	t; // QUESTION: `t` should be moved already, but why can we still use it?
}

It doesn't copy, it reborrows. Here's a short introduction.

3 Likes

Just think about it – that would be impossible to implement soundly. Rust requires that no independent mutable references exist to the same place at the same time.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.