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:
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:
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:
Size of bool: 1 byte
We can do this for all other primitive types in Rust. An example is provided below:
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 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:
- The algorithm adds the size of the field in the order of declaration.
- It rounds up the size to the nearest multiple of the next field’s alignment.
- 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:
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:
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:
The previous example determines the size of an array of i32 types and a length of 5. The equivalent array is as shown below:
The program should return the size of the array as:
Rust Size of Pointer
The following example evaluates the size of a pointer using the syntax:
The previous code returns the size of a raw pointer in an i32 reference. The resulting output is provided below:
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.