此代码(also on play)
use std::sync::Arc;
struct Foo {
x: isize, // Something complex in actual code, implements Drop
}
#[derive(Clone)]
struct Good {
a: Option<Arc<Foo>>,
b: Option<Arc<Foo>>,
c: Option<Arc<Foo>>,
}
#[derive(Clone)]
struct Bad {
x: [Option<Arc<Foo>>; 3],
}
fn main() {
println!("See?");
}
无效Bad
<anon>:16:5: 16:29 error: the trait `core::marker::Copy` is not implemented for the type `alloc::arc::Arc<Foo>` [E0277]
<anon>:16 x: [Option<Arc<Foo>>; 3],
^~~~~~~~~~~~~~~~~~~~~~~~
<anon>:14:10: 14:15 note: in expansion of #[derive_Clone]
但Good
没有问题。
答案 0 :(得分:4)
问题出在implementation of the Clone
trait:
impl<T> Clone for [T; 4] where T: Copy
这使得真正的问题:为什么我们需要Copy
来克隆数组? That implementation说:
fn clone(&self) -> [T; $N] {
*self
}
目前,数组的克隆只是源数组的逐位复制。更深入的为什么可能来自更有知识的人。