In Rust, we all know that Vec is not Copy, which means it does not implement the Copy trait. As a result, after iterating over a Vec like this:
let num = vec![1, 2, 3];
for i in num {
println!("{}", i);
}
println!("{:?}", num); // compiler error: value borrowed here after move
the compiler reports an error because num has been moved. When we write for i in num, Rust desugars it into something like for i in num.into_iter(). The into_iter method takes self by value:
#[inline]
fn into_iter(self) -> Self::IntoIter {
// ...
}
Because Vec<T> is not Copy, passing it by value moves it into the iterator. Ownership is transferred to the iterator, so num can no longer be used after the loop.
Now letβs try the same thing with an array:
let num = [1, 2, 3];
for i in num {
println!("{}", i);
}
println!("{:?}", num); // works
There is no compiler error this time. Why?
The type of num is [i32; 3]. Since i32 implements Copy, the array also implements Copy. That means num is copied into the loop instead of being moved, and the original array remains available after the loop.
Now consider an array of Strings:
let strs = ["one".to_string(), "two".to_string()];
for s in strs {
println!("{}", s);
}
println!("{:?}", strs); // compiler error: value borrowed here after move
This produces the same compiler error as the Vec example.
The reason is that String does not implement Copy. As a result[String; 2] is also not Copy. When the array is passed to into_iter, it is moved into the iterator, making strs unavailable afterward.
So how does Rust know that some arrays are Copy while others are not?
This isnβt compiler magic. Itβs implemented in the standard library with a blanket implementation:
impl<T: Copy, const N: usize> Copy for [T; N] {}
This implementation says:
For any type T that implements Copy, and for any array length N, the array [T; N] also implements Copy.
As a result:
-
[i32; 3]isCopybecausei32isCopy. -
[bool; 10]isCopybecauseboolisCopy. -
[String; 2]is notCopybecauseStringis notCopy.
In other words, whether an array implements Copy depends entirely on whether its element type implements Copy.
Top comments (0)