8
8
// option. This file may not be copied, modified, or distributed
9
9
// except according to those terms.
10
10
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))))`.
12
45
13
46
#![ stable]
14
47
@@ -29,8 +62,8 @@ use core::raw::TraitObject;
29
62
use core:: result:: Result :: { Ok , Err } ;
30
63
use core:: result:: Result ;
31
64
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.
34
67
///
35
68
/// The following two examples are equivalent:
36
69
///
@@ -39,23 +72,29 @@ use core::result::Result;
39
72
/// use std::boxed::HEAP;
40
73
///
41
74
/// 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;
46
77
/// }
47
78
/// ```
48
79
#[ lang = "exchange_heap" ]
49
80
#[ unstable = "may be renamed; uncertain about custom allocator design" ]
50
81
pub static HEAP : ( ) = ( ) ;
51
82
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.
53
86
#[ lang = "owned_box" ]
54
87
#[ stable]
55
88
pub struct Box < T > ( Unique < T > ) ;
56
89
57
90
impl < 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
+ /// ```
59
98
#[ stable]
60
99
pub fn new ( x : T ) -> Box < T > {
61
100
box x
@@ -76,11 +115,29 @@ impl<T> Default for Box<[T]> {
76
115
77
116
#[ stable]
78
117
impl < 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
+ /// ```
80
126
#[ inline]
81
127
fn clone ( & self ) -> Box < T > { box { ( * * self ) . clone ( ) } }
82
128
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
+ /// ```
84
141
#[ inline]
85
142
fn clone_from ( & mut self , source : & Box < T > ) {
86
143
( * * self ) . clone_from ( & ( * * source) ) ;
0 commit comments