Back
+20 XP
6/12
Challenge: Manual Borsh-Style Serialization in Rust
To truly understand how Borsh works under the hood, you'll manually serialize account data to bytes using Rust's standard library.
Your Task
Implement serialize_account(owner, balance, frozen) that converts account fields to a Vec<u8> with this Borsh-like layout:
- 4 bytes: owner name length as
u32(little-endian) - N bytes: owner name as UTF-8 bytes
- 8 bytes: balance as
u64(little-endian) - 1 byte: frozen flag (
0= false,1= true)
Requirements:
- Use
u32::to_le_bytes()for the length prefix - Use
.as_bytes()to get UTF-8 bytes from a&str - Use
u64::to_le_bytes()for the balance - Push
0u8or1u8for the boolean - Total size should be
4 + owner.len() + 8 + 1bytes
Rust APIs:
Vec::new(),Vec::extend_from_slice()u32::to_le_bytes(),u64::to_le_bytes()str::as_bytes(),str::len()
Test Cases
Serialized size should be 4 + 5 + 8 + 1 = 18 bytes
Input:
"Alice", 1000, falseExpected: __result__.len() == 18Size should change with owner name length (4 + 3 + 8 + 1)
Input:
"Bob", 42, trueExpected: __result__.len() == 16