Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Is this optimization required by the language semantics or some standard (vs. being only a description of mainline rustc's implemented optimizations)?


AFAIK there is no prescriptive standard for Rust, there's only descriptions of how the rustc compiler works. But this was a very intentional decision quite some time ago, and is also the reason why the `std::ptr::NonNull` type exists (the documentation explicitly states this allows enums to use the forbidden value as a discriminant).

FWIW the module documentation of `std::option` does describe this optimization for Option<Box<T>>.

> This usage of Option to create safe nullable pointers is so common that Rust does special optimizations to make the representation of Option<Box<T>> a single pointer. Optional pointers in Rust are stored as efficiently as any other pointer type.


It’s a defined part of the language semantics.


Is there actually a formal language semantics definition anywhere? I thought all we had was stuff like https://doc.rust-lang.org/reference/index.html which is explicitly listed as "not a formal spec".


Things have different degrees of guarantees. This is something we’ve explicitly said is guaranteed.


Where are things like this said/defined/collated?


We are generally at a point where if it's documented, it's guaranteed. This feature is really easy to describe, and so we have a high degree of confidence that when we say "this behavior is defined", we won't run into an edge case where we'd have to break it.

So, in this case, it's a documented part of Option.

I realize this answer isn't fantastic, but it's where we're at now. It's why we're working on the reference this year!


>we're working on the reference this year!

I didn't know this, it's good to hear. Thanks!




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: