Search Results


Sizes 101

Date Apr 07, 2021

Sizes are an important topic to discuss, because at the end of the day while creating some arbitrary data structure you are going to be in trouble if dealing with FFIs or foreign interfaces. What I have done here is to preassign few data types that I use frequently.

Let's start with a simple binary. Suppose, you are going to create a very light structure with predicable memory size, we will call it Baby.

pub struct Baby{
    head: bool,
    limbs: u8
}

The problem arises here is we do not know the size of this structure at the runtime. Now, there are literally so many solutions to this problem but the key tip here, for this is to simple use the Rust's builtin library std::mem.

fn main() {
    let size_obj = std::mem::size_of::<Baby>();
    println!("Your baby is {} long. Such cuteness!", size_obj);
}

Try this and you will get the size for the Baby. Doesn’t matter if its a baby of Human or a Dog or a Tiger. Size does matter, that's what she said 😆

If you like this tip, then check out my other entries of this series here

Or if you love Rust language like I do then read my entries here