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.
United States
NORTH AMERICA
Related News
Secret Claude Tracker Shocks Users After Anthropic's Anti-Surveillance Stance
12h ago
EV Batteries Defy Expectations, Last Hundreds of Thousands of Miles
1d ago
GBase 8a Performance Anomaly Case Study: How a Single Parameter Change Sparked a Chain Reaction
1d ago
Who Else Has Inherited a Codebase With Zero Comments and a Prayer?
1d ago
完美的平庸
3h ago