Struct 如何实现自定义结构的复制特性?

Struct 如何实现自定义结构的复制特性?,struct,rust,traits,Struct,Rust,Traits,我有我的自定义结构事务,我想我可以复制它 #[derive(PartialOrd, Eq, Copy, Clone, Hash)] struct Transaction { transaction_id: [u8; 2], proto_id: [u8; 2], len_field: [u8; 2], unit_id: u8, func_nr: u8, count_bytes: u8, } impl PartialEq for Transacti

我有我的自定义结构事务,我想我可以复制它

#[derive(PartialOrd, Eq, Copy, Clone, Hash)]
struct Transaction {
    transaction_id: [u8; 2],
    proto_id:  [u8; 2],
    len_field: [u8; 2],
    unit_id: u8,
    func_nr: u8,
    count_bytes: u8,
}

impl PartialEq for Transaction {
    fn eq(&self, other: &Self) -> bool {
        self.unit_id == other.unit_id
            && self.func_nr == other.func_nr
            && self.count_bytes == other.count_bytes
    }
}
这会失败,因为Vec不为任何T实现复制

如何实现复制到Vec和my struct。我要举个例子

#[派生(PartialOrd、Eq、Hash)]
结构事务{
交易编号:Vec,
原始id:Vec,
len_字段:Vec,
单位识别号:u8,
职能编号:u8,
计数字节:u8,
}
事务{}的impl副本
事务的impl克隆{
fn克隆(&self)->事务{
*自我
}
}
事务的impl PartialEq{
fn eq(&self,其他:&self)->bool{
self.unit\u id==其他.unit\u id
&&self.func\u nr==其他.func\u nr
&&self.count\u字节==其他.count\u字节
}
}
fn main()
{
}
我解决了这个问题: 我使用表[u8;2]而不是Vec


但我还是不明白为什么不能在结构中使用向量并复制它

#[derive(PartialOrd, Eq, Copy, Clone, Hash)]
struct Transaction {
    transaction_id: [u8; 2],
    proto_id:  [u8; 2],
    len_field: [u8; 2],
    unit_id: u8,
    func_nr: u8,
    count_bytes: u8,
}

impl PartialEq for Transaction {
    fn eq(&self, other: &Self) -> bool {
        self.unit_id == other.unit_id
            && self.func_nr == other.func_nr
            && self.count_bytes == other.count_bytes
    }
}

每次使用事务结构时,每次将其传递给函数时,是否确实要复制三个向量?你真正的问题是什么?复制结构不涉及使用
Copy
特性。您是指
Copy
还是
Clone
,在《铁锈》中有些不同。@edwardw我不认为这是重复的,因为在我看来这是一个XY问题。@DenysSéguret在我看来,这个问题的答案也回答了这个问题。文档显示,“复制”Vec特性没有实现。我明白这应该实现。“但我仍然不明白为什么不能在结构中使用向量并复制它。”只要阅读副本--