Why is Box special?

⚓ Rust    📅 2026-01-13    👤 surdeus    👁️ 1      

surdeus

Info

This post is auto-generated from RSS feed The Rust Programming Language Forum - Latest topics. Source: Why is Box special?

It 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

Read full topic

🏷️ Rust_feed