DEV Community

Jazz Thumyat πŸ¦€
Jazz Thumyat πŸ¦€

Posted on

How Rust Decides Whether an Array Is Copy

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
Enter fullscreen mode Exit fullscreen mode

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 {
    // ...
}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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] {}
Enter fullscreen mode Exit fullscreen mode

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] is Copy because i32 is Copy.
  • [bool; 10] is Copy because bool is Copy.
  • [String; 2] is not Copy because String is not Copy.

In other words, whether an array implements Copy depends entirely on whether its element type implements Copy.

Top comments (0)