As an addition to your code, you can always ask the compiler to tell you if a type can be copied, even without being able to construct that type:
fn is_this_type_copy<T: Copy>() {}
fn main() {
is_this_type_copy::<&u8>();
}
If a type does not implement Copy
, the compiler will produce an error.
You can extend this to ask the question for every reference to a type. Your existing code only shows that an immutable reference to a specific type implements Copy
:
fn is_every_reference_copy<T>() {
is_this_copy::<&T>()
}
Doing the same thing for &mut T
:
fn is_every_mut_reference_copy<T>() {
is_this_copy::<&mut T>()
}
Produces the same error you saw:
error[E0277]: the trait bound `&mut T: std::marker::Copy` is not satisfied
--> src/main.rs:8:5
|
8 | is_this_copy::<&mut T>()
| ^^^^^^^^^^^^^^^^^^^^^^ the trait `std::marker::Copy` is not implemented for `&mut T`
|
= note: required by `is_this_copy`
We've already seen why &mut T
cannot be copied, but why can &T
be copied? In a way, that's kind of the entire point of a reference. A reference is a way of cheaply sharing data. Instead of needing to clone (potentially expensively) something to give to multiple things, we can simply give each thing a lightweight reference to the original thing. If references couldn't be copied, they wouldn't have nearly the same amount of value.