In Rust, references can never be null, so in case where you actually need null, such as a linked list, you use the Option type:
struct Element {
Yes, there is some compiler magic that optimises Option to a single pointer (most of the time).
use std::mem::size_of;
macro_rules! show_size {
(header) => (
println!("{:<22} {:>4} {}", "Type", "T", "Option");
);
($t:ty) => (
println!("{:<22} {:4} {:4}", stringify!($t), size_of::<$t>(), size_of::
The following sizes are printed (on a 64-bit machine, so pointers are 8 bytes):
// As of Rust 1.22.1
Type T Option
i32 4 8
&i32 8 8
Box 8 8
&[i32] 16 16
Vec 24 24
Result<(), Box> 8 16
Note that &i32, Box, &[i32], Vec all use the non-nullable pointer optimization inside an Option!