Skip to content

Commit 77fee7d

Browse files
committed
Rollup merge of #24629 - steveklabnik:gh24511, r=alexcrichton
Without the `box` keyword, one of these two reasons is not correct, so let's just eliminate this section and elaborate on the reason for the legit use case inline. Fixes #24511
2 parents 5de4e87 + ac09864 commit 77fee7d

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

src/liballoc/boxed.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,9 @@
1010

1111
//! A pointer type for heap allocation.
1212
//!
13-
//! `Box<T>`, casually referred to as a 'box', provides the simplest form of
14-
//! heap allocation in Rust. Boxes provide ownership for this allocation, and
15-
//! drop their contents when they go out of scope.
16-
//!
17-
//! Boxes are useful in two situations: recursive data structures, and
18-
//! occasionally when returning data. [The Pointer chapter of the
19-
//! Book](../../../book/pointers.html#best-practices-1) explains these cases in
20-
//! detail.
13+
//! `Box<T>`, casually referred to as a 'box', provides the simplest form of heap allocation in
14+
//! Rust. Boxes provide ownership for this allocation, and drop their contents when they go out of
15+
//! scope.
2116
//!
2217
//! # Examples
2318
//!
@@ -43,6 +38,16 @@
4338
//! ```
4439
//!
4540
//! This will print `Cons(1, Box(Cons(2, Box(Nil))))`.
41+
//!
42+
//! Recursive structures must be boxed, because if the definition of `Cons` looked like this:
43+
//!
44+
//! ```rust,ignore
45+
//! Cons(T, List<T>),
46+
//! ```
47+
//!
48+
//! It wouldn't work. This is because the size of a `List` depends on how many elements are in the
49+
//! list, and so we don't know how much memory to allocate for a `Cons`. By introducing a `Box`,
50+
//! which has a defined size, we know how big `Cons` needs to be.
4651
4752
#![stable(feature = "rust1", since = "1.0.0")]
4853

0 commit comments

Comments
 (0)