“该str类型也称为‘字符串切片’,是最原始的[强调]字符串类型。” (https://doc.rust-lang.org/std/primitive.str.html)
因此,直观上str应该是可复制的,它是:
fn main() {
let _str = "hello";
let _str2 = _str;
println!("{}", _str); // Output: hello
}
Run Code Online (Sandbox Code Playgroud)
但是,它没有实现以下Copy特征:
fn main() {
is_copy::<str>(); // Compile time error: the trait std::marker::Copy is not implemented for str
}
fn is_copy<T: Copy>() {}
Run Code Online (Sandbox Code Playgroud)
是什么允许这种类似复制的行为str?
在 Rust 中,变量默认是不可变的,即它们不会变化,但不是常量(如此处所述)。
他们只是按照惯例保留名称“变量”,还是有其他原因保留术语“变量”?