Rust Lang

How to Use the size_of Function in Rust

In Rust, we can determine the size of a specific type using the size_of function. This function is in core::mem::size_of and takes a type and returns its size in bytes.

Let us learn how to use the size_of function to determine the size of various types in Rust.

Rust Size of Primitive Types

We can determine the size of the Rust primitive types using the size_of function. The general syntax of the size_of function is as shown below:

pub const fn size_of<T>()

As mentioned, the function will return the size of the specified type in Bytes. Rust states the size of a type is not stable in cross compilations except for primitive types.

Let us use the following example to determine the size of a Boolean type:

use std::mem;

fn main() {

println!("Size of bool: {} byte", mem::size_of::<bool>());

}

The example program imports the memory module from the standard library. This gives us access to the size_of function to determine the size of a bool.

Then, we call the function inside the println method and return the size of a Boolean type.

The resulting output is as shown below:

$ cargo run

Size of bool: 1 byte

We can do this for all other primitive types in Rust. An example is provided below:

use std::mem;

fn main() {

println!("Size of bool: {} byte", mem::size_of::<bool>());

println!("Size of 8-bit unsigned integer: {} byte", mem::size_of::<u8>());

println!("Size of 16-bit unsigned integer: {} bytes", mem::size_of::<u16>());

println!("Size of 32-bit unsigned integer: {} bytes", mem::size_of::<u32>());

println!("Size of 64-bit unsigned integer: {} bytes", mem::size_of::<u64>());

println!("Size of 8-bit signed integer: {} byte", mem::size_of::<i8>());

println!("Size of 16-bit signed integer: {} bytes", mem::size_of::<i16>());

println!("Size of 32-bit signed integer: {} bytes", mem::size_of::<i32>());

println!("Size of 64-bit signed integer: {} bytes", mem::size_of::<i64>());

println!("Size of 32-bit floating type: {} bytes", mem::size_of::<f32>());

println!("Size of 64-bit floating type: {} bytes", mem::size_of::<i64>());

println!("Size of character: {} bytes", mem::size_of::<char>());

}

The above-mentioned simple program determines the size of all primitive types in the Rust language. An example resulting output is as shown:

Size of bool: 1 byte

Size of 8-bit unsigned integer: 1 byte

Size of 16-bit unsigned integer: 2 bytes

Size of 32-bit unsigned integer: 4 bytes

Size of 64-bit unsigned integer: 8 bytes

Size of 8-bit signed integer: 1 byte

Size of 16-bit signed integer: 2 bytes

Size of 32-bit signed integer: 4 bytes

Size of 64-bit signed integer: 8 bytes

Size of 32-bit floating type: 4 bytes

Size of 64-bit floating type: 8 bytes

Size of character: 4 bytes

Rust Size of Struct

In Rust, the size of structs is determined by a simple algorithm:

  1. The algorithm adds the size of the field in the order of declaration.
  2. It rounds up the size to the nearest multiple of the next field’s alignment.
  3. Finally, the algorithm rounds the size of the struct to the nearest multiple of its alignment.

You can learn more about the alignment in the resource provided below:

https://doc.rust-lang.org/stable/std/mem/fn.align_of.html

An example of the size of a struct is as shown below:

struct User {
    name: String,
    age: u32,
    email: String,
}
fnmain() {
    println!("Size of the struct: {} bytes", mem::size_of::())
}

The previous code should return the size of the struct as:

$ cargo run

Size of the struct: 56 bytes

Rust Size of Array

We can also use the size_of method to determine the size of an array. Consider the example provided below:

println!("Size of array is: {} bytes", mem::size_of::<[i32; 5]>())

The previous example determines the size of an array of i32 types and a length of 5. The equivalent array is as shown below:

let my_array: [i32; 5] = [1,2,3,4,5];

The program should return the size of the array as:

Size of array is: 20 bytes

Rust Size of Pointer

The following example evaluates the size of a pointer using the syntax:

println!("Size of pointer {} bytes", mem::size_of::<*const i32>());

The previous code returns the size of a raw pointer in an i32 reference. The resulting output is provided below:

Size of pointer8 bytes

Conclusion

This article describes how to determine the size of various types in Rust, including primitive types, arrays, pointers, and structs. Also, the size_of function was discussed in detail. We hope you found this article helpful. Check the other Linux Hint article for more tips and articles.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list