tl;dr: Avoid Box<T>
in general.
Actually, this rule is so important that the Rust Pointer Guide explicitly says the same. Therefore without a further ado, you should avoid Box<T>
except for these three cases:
-
When you absolutely need a trait object (
Box<Trait>
). But review carefully to see if it is indeed absolutely needed; you may try to generalize things too far, for example. -
When you have a recursive data structure. This may be mandatory when you have an inherently recursive data (e.g. scene graph), but it may also be a sign of the premature optimization. Again, review carefully to see if you need to write a separate data structure yourself, and use the
collection
crate if possible.