Why is Box special?
⚓ Rust 📅 2026-01-13 👤 surdeus 👁️ 1It seems like in most cases Box<T> is just a pointer to heap allocated memory. But Box<str> seems to be a special case where the str is "inlined" and holds both a pointer and a length. So for example Box<u8> is 8 bytes but Box<str> is 16-bytes (assuming a 64-bit architecture).
I can see that this is a good optimisation but how is it implemented? In the standard library or as a special case in the compiler? Is it documented anywhere? Can not find any mention of it in "the book", the reference or in standard library documentation.
/Mikael
7 posts - 4 participants
🏷️ Rust_feed