2 ms·
> Kinda like Rust guarantees that Option<T> has the same size as T (aka free like in free beer). That is not correct. Trivial counterexample: https://play.rus
by zokier 5y ago
> Kinda like Rust guarantees that Option<T> has the same size as T (aka free like in free beer).
That is not correct. Trivial counterexample:
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=941b8b2833b2537af9c2eac82d89980e https://play.rust-lang.org/?version=stable&mode=debug&editio...
prints
4
8
i.e. Option<T> is 4 bytes larger than plain T
- deepsun 5y agoSure, I meant: Rust guarantees to optimize the following types T such that Option<T> has the same size as T: Box<U> &U &mut U fn, extern "C" fn num::NonZero* ptr::NonNull<U> #[repr(transparent)] struct around one of the types in this list. UPDATE: Or, better per your example: struct T {i: i32} takes the same size as i32: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=b95205ebeff6ddbe08a0b9d9900bb260 https://play.rust-lang.org/?version=stable&mode=debug&editio...