Skip to content

Commit 6769903

Browse files
ftxqxdalexcrichton
authored andcommitted
---
yaml --- r: 124840 b: refs/heads/auto c: 37bb6ed h: refs/heads/master v: v3
1 parent fcc77a0 commit 6769903

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ refs/heads/try3: 9387340aab40a73e8424c48fd42f0c521a4875c0
1313
refs/tags/release-0.3.1: 495bae036dfe5ec6ceafd3312b4dca48741e845b
1414
refs/tags/release-0.4: e828ea2080499553b97dfe33b3f4d472b4562ad7
1515
refs/tags/release-0.5: 7e3bcfbf21278251ee936ad53e92e9b719702d73
16-
refs/heads/auto: 36e1f2db301d33fd0efaa093c5a190a3879ccc93
16+
refs/heads/auto: 37bb6ed30294d3847386b284f39f90fd245f9be9
1717
refs/heads/servo: af82457af293e2a842ba6b7759b70288da276167
1818
refs/tags/release-0.6: b4ebcfa1812664df5e142f0134a5faea3918544c
1919
refs/tags/0.1: b19db808c2793fe2976759b85a355c3ad8c8b336

branches/auto/src/libcollections/vec.rs

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,27 @@ pub static PTR_MARKER: u8 = 0;
5151
/// The `vec!` macro is provided to make initialization more convenient:
5252
///
5353
/// ```rust
54-
/// let mut vec = vec!(1i, 2i, 3i);
54+
/// let mut vec = vec![1i, 2i, 3i];
5555
/// vec.push(4);
56-
/// assert_eq!(vec, vec!(1, 2, 3, 4));
56+
/// assert_eq!(vec, vec![1, 2, 3, 4]);
5757
/// ```
58+
///
59+
/// # Capacity and reallocation
60+
///
61+
/// The capacity of a vector is the amount of space allocated for any future
62+
/// elements that will be added onto the vector. This is not to be confused
63+
/// with the *length* of a vector, which specifies the number of actual
64+
/// elements within the vector. If a vector's length exceeds its capacity,
65+
/// its capacity will automatically be increased, but its elements will
66+
/// have to be reallocated.
67+
///
68+
/// For example, a vector with capacity 10 and length 0 would be an empty
69+
/// vector with space for 10 more elements. Pushing 10 or fewer elements onto
70+
/// the vector will not change its capacity or cause reallocation to occur.
71+
/// However, if the vector's length is increased to 11, it will have to
72+
/// reallocate, which can be slow. For this reason, it is recommended
73+
/// to use `Vec::with_capacity` whenever possible to specify how big the vector
74+
/// is expected to get.
5875
#[unsafe_no_drop_flag]
5976
pub struct Vec<T> {
6077
len: uint,
@@ -87,11 +104,28 @@ impl<T> Vec<T> {
87104
/// The vector will be able to hold exactly `capacity` elements without
88105
/// reallocating. If `capacity` is 0, the vector will not allocate.
89106
///
107+
/// It is important to note that this function does not specify the
108+
/// *length* of the returned vector, but only the *capacity*. (For an
109+
/// explanation of the difference between length and capacity, see
110+
/// the main `Vec` docs above, 'Capacity and reallocation'.) To create
111+
/// a vector of a given length, use `Vec::from_elem` or `Vec::from_fn`.
112+
///
90113
/// # Example
91114
///
92115
/// ```rust
93116
/// # use std::vec::Vec;
94-
/// let vec: Vec<int> = Vec::with_capacity(10);
117+
/// let mut vec: Vec<int> = Vec::with_capacity(10);
118+
///
119+
/// // The vector contains no items, even though it has capacity for more
120+
/// assert_eq!(vec.len(), 0);
121+
///
122+
/// // These are all done without reallocating...
123+
/// for i in range(0u, 10) {
124+
/// vec.push(i);
125+
/// }
126+
///
127+
/// // ...but this may make the vector reallocate
128+
/// vec.push(11);
95129
/// ```
96130
#[inline]
97131
pub fn with_capacity(capacity: uint) -> Vec<T> {

0 commit comments

Comments
 (0)