I have this code:
struct Chunk{
chunk_type: ChunkType,
data: Vec<u8>,
crc: u32
}
impl Chunk {
fn new(chunk_type: ChunkType, data: Vec<u8>) -> Self {
let crc = Crc::<u32>::new(&CRC_32_CKSUM);
Chunk {
crc: crc.checksum(&[&chunk_type.bytes()[..], &data].concat()),
chunk_type,
data
}
}
}
It compiles just fine. However, if I just change the order of params in the initialization of Chunk
, I get the classic "borrow of a moved value" error:
impl Chunk {
fn new(chunk_type: ChunkType, data: Vec<u8>) -> Self {
let crc = Crc::<u32>::new(&CRC_32_CKSUM);
Chunk {
chunk_type, // value moved here
data,
crc: crc.checksum(&[&chunk_type.bytes()[..], &data].concat()) // borrow of moved value
}
}
}
I wonder, why would it throw such an error in this case? Won't the value of crc
property be calculated before any move occurs?