88// option. This file may not be copied, modified, or distributed
99// except according to those terms.
1010
11- //! A unique pointer type.
11+ //! A pointer type for heap allocation.
12+ //!
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.
16+ //!
17+ //! Boxes are useful in two situations: recursive data structures, and occasionally when returning
18+ //! data. [The Pointer chapter of the Book](../../../book/pointers.html#best-practices-1) explains
19+ //! these cases in detail.
20+ //!
21+ //! # Examples
22+ //!
23+ //! Creating a box:
24+ //!
25+ //! ```
26+ //! let x = Box::new(5);
27+ //! ```
28+ //!
29+ //! Creating a recursive data structure:
30+ //!
31+ //! ```
32+ //! #[derive(Show)]
33+ //! enum List<T> {
34+ //! Cons(T, Box<List<T>>),
35+ //! Nil,
36+ //! }
37+ //!
38+ //! fn main() {
39+ //! let list: List<i32> = List::Cons(1, Box::new(List::Cons(2, Box::new(List::Nil))));
40+ //! println!("{:?}", list);
41+ //! }
42+ //! ```
43+ //!
44+ //! This will print `Cons(1i32, Box(Cons(2i32, Box(Nil))))`.
1245
1346#![ stable]
1447
@@ -29,8 +62,8 @@ use core::raw::TraitObject;
2962use core:: result:: Result :: { Ok , Err } ;
3063use core:: result:: Result ;
3164
32- /// A value that represents the global exchange heap. This is the default
33- /// place that the `box` keyword allocates into when no place is supplied.
65+ /// A value that represents the heap. This is the default place that the `box` keyword allocates
66+ /// into when no place is supplied.
3467///
3568/// The following two examples are equivalent:
3669///
@@ -39,23 +72,29 @@ use core::result::Result;
3972/// use std::boxed::HEAP;
4073///
4174/// fn main() {
42- /// # struct Bar;
43- /// # impl Bar { fn new(_a: int) { } }
44- /// let foo = box(HEAP) Bar::new(2);
45- /// let foo = box Bar::new(2);
75+ /// let foo = box(HEAP) 5;
76+ /// let foo = box 5;
4677/// }
4778/// ```
4879#[ lang = "exchange_heap" ]
4980#[ unstable = "may be renamed; uncertain about custom allocator design" ]
5081pub static HEAP : ( ) = ( ) ;
5182
52- /// A type that represents a uniquely-owned value.
83+ /// A pointer type for heap allocation.
84+ ///
85+ /// See the [module-level documentation](../../std/boxed/index.html) for more.
5386#[ lang = "owned_box" ]
5487#[ stable]
5588pub struct Box < T > ( Unique < T > ) ;
5689
5790impl < T > Box < T > {
58- /// Moves `x` into a freshly allocated box on the global exchange heap.
91+ /// Allocates memory on the heap and then moves `x` into it.
92+ ///
93+ /// # Examples
94+ ///
95+ /// ```
96+ /// let x = Box::new(5);
97+ /// ```
5998 #[ stable]
6099 pub fn new ( x : T ) -> Box < T > {
61100 box x
@@ -76,11 +115,29 @@ impl<T> Default for Box<[T]> {
76115
77116#[ stable]
78117impl < T : Clone > Clone for Box < T > {
79- /// Returns a copy of the owned box.
118+ /// Returns a new box with a `clone()` of this box's contents.
119+ ///
120+ /// # Examples
121+ ///
122+ /// ```
123+ /// let x = Box::new(5);
124+ /// let y = x.clone();
125+ /// ```
80126 #[ inline]
81127 fn clone ( & self ) -> Box < T > { box { ( * * self ) . clone ( ) } }
82128
83- /// Performs copy-assignment from `source` by reusing the existing allocation.
129+ /// Copies `source`'s contents into `self` without creating a new allocation.
130+ ///
131+ /// # Examples
132+ ///
133+ /// ```
134+ /// let x = Box::new(5);
135+ /// let mut y = Box::new(10);
136+ ///
137+ /// y.clone_from(&x);
138+ ///
139+ /// assert_eq!(*y, 5);
140+ /// ```
84141 #[ inline]
85142 fn clone_from ( & mut self , source : & Box < T > ) {
86143 ( * * self ) . clone_from ( & ( * * source) ) ;
0 commit comments