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 {
value: i32,
next: Option<Box<Element>>,
}
How much overhead is involved in this in terms of memory allocation and steps to dereference compared to a simple pointer? Is there some "magic" in the compiler/runtime to make Option
cost-free, or less costly than if one were to implement Option
by oneself in a non-core library using the same enum
construct, or by wrapping the pointer in a vector?