"The str type, also called a 'string slice', is the most primitive [emphasis added] string type." (https://doc.rust-lang.org/std/primitive.str.html)
Intuitively str should therefore be copiable, which it is:
fn main() {
let _str = "hello";
let _str2 = _str;
println!("{}", _str); // Output: hello
}
However, it does not implement the Copy trait:
fn main() {
is_copy::<str>(); // Compile time error: the trait std::marker::Copy is not implemented for str
}
fn is_copy<T: Copy>() {}
What allows this copy-like behaviour with str?
"abc"is more than just astr. It is in fact a reference:Playground.
Therefore, we can't look at the
strimplementations directly, instead we must look at the&T(Reference) implementations of traits.We have a copy impl:
This satisfies
&str. But sincestris unsized, we cannot impl copy for it, since it is the data in a string, not a pointer/reference/(size, ptr)to it. We could therefore not do a bitwiseCopyof thestrsince we wouldn't know how much data to copy.