expression

A documented niche for optional references

A niche is a representation unavailable to a valid value of a type. For sized referents the standard library guarantees Option<&T> has the same size, alignment and function-call ABI as &T. Use Option constructors rather than fabricating references from bytes. Expected stdout (not executed): true true true Common mistake: Assuming every Option<T> uses the same representation as T.

Formal Syntax & Structure

rust syntaxrust
Option<&T>

Working Example

Example Snippetrust
#![deny(unsafe_op_in_unsafe_fn)]fn main() {    use std::mem::size_of;    let x = 7_u32;    let present = Some(&x);    let absent: Option<&u32> = None;    println!("{} {} {}", size_of::<Option<&u32>>() == size_of::<&u32>(), present.copied() == Some(7), absent.is_none());}
Rust Reference