How to get size of struct fields in Rust? [duplicate]
We can use the std::mem::size_of_val and pass the reference of each fields. The returned type is usize
which itself can be 4 or 8 bytes based on the target machine.
let entry = MemTableEntry{
key: b"Hello".to_vec(),
value: Some(b"World".to_vec()),
timestamp: 123u128,
deleted: false
};
let size = mem::size_of_val(&entry.key)
+ mem::size_of_val(&entry.value)
+ mem::size_of_val(&entry.timestamp)
+ mem::size_of_val(&entry.deleted);
assert_eq!(65, size);
Note: Please look at the @Caesar's comment below about memory.