Skip to content

Commit 18f9758

Browse files
Rollup merge of rust-lang#37043 - GuillaumeGomez:vec_urls, r=frewsxcv
Add missing urls on Vec docs r? @steveklabnik
2 parents 5509ae3 + 599ad8e commit 18f9758

File tree

1 file changed

+85
-52
lines changed

1 file changed

+85
-52
lines changed

src/libcollections/vec.rs

Lines changed: 85 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@
1616
//!
1717
//! # Examples
1818
//!
19-
//! You can explicitly create a `Vec<T>` with `new()`:
19+
//! You can explicitly create a [`Vec<T>`] with [`new()`]:
2020
//!
2121
//! ```
2222
//! let v: Vec<i32> = Vec::new();
2323
//! ```
2424
//!
25-
//! ...or by using the `vec!` macro:
25+
//! ...or by using the [`vec!`] macro:
2626
//!
2727
//! ```
2828
//! let v: Vec<i32> = vec![];
@@ -32,7 +32,7 @@
3232
//! let v = vec![0; 10]; // ten zeroes
3333
//! ```
3434
//!
35-
//! You can `push` values onto the end of a vector (which will grow the vector
35+
//! You can [`push`] values onto the end of a vector (which will grow the vector
3636
//! as needed):
3737
//!
3838
//! ```
@@ -49,13 +49,20 @@
4949
//! let two = v.pop();
5050
//! ```
5151
//!
52-
//! Vectors also support indexing (through the `Index` and `IndexMut` traits):
52+
//! Vectors also support indexing (through the [`Index`] and [`IndexMut`] traits):
5353
//!
5454
//! ```
5555
//! let mut v = vec![1, 2, 3];
5656
//! let three = v[2];
5757
//! v[1] = v[1] + 5;
5858
//! ```
59+
//!
60+
//! [`Vec<T>`]: ../../std/vec/struct.Vec.html
61+
//! [`new()`]: ../../std/vec/struct.Vec.html#method.new
62+
//! [`push`]: ../../std/vec/struct.Vec.html#method.push
63+
//! [`Index`]: ../../std/ops/trait.Index.html
64+
//! [`IndexMut`]: ../../std/ops/trait.IndexMut.html
65+
//! [`vec!`]: ../../std/macro.vec.html
5966
6067
#![stable(feature = "rust1", since = "1.0.0")]
6168

@@ -79,7 +86,7 @@ use core::slice;
7986
use super::SpecExtend;
8087
use super::range::RangeArgument;
8188

82-
/// A contiguous growable array type, written `Vec<T>` but pronounced 'vector.'
89+
/// A contiguous growable array type, written `Vec<T>` but pronounced 'vector'.
8390
///
8491
/// # Examples
8592
///
@@ -105,7 +112,7 @@ use super::range::RangeArgument;
105112
/// assert_eq!(vec, [7, 1, 2, 3]);
106113
/// ```
107114
///
108-
/// The `vec!` macro is provided to make initialization more convenient:
115+
/// The [`vec!`] macro is provided to make initialization more convenient:
109116
///
110117
/// ```
111118
/// let mut vec = vec![1, 2, 3];
@@ -137,15 +144,15 @@ use super::range::RangeArgument;
137144
///
138145
/// # Indexing
139146
///
140-
/// The Vec type allows to access values by index, because it implements the
141-
/// `Index` trait. An example will be more explicit:
147+
/// The `Vec` type allows to access values by index, because it implements the
148+
/// [`Index`] trait. An example will be more explicit:
142149
///
143150
/// ```
144151
/// let v = vec!(0, 2, 4, 6);
145152
/// println!("{}", v[1]); // it will display '2'
146153
/// ```
147154
///
148-
/// However be careful: if you try to access an index which isn't in the Vec,
155+
/// However be careful: if you try to access an index which isn't in the `Vec`,
149156
/// your software will panic! You cannot do this:
150157
///
151158
/// ```ignore
@@ -158,7 +165,7 @@ use super::range::RangeArgument;
158165
///
159166
/// # Slicing
160167
///
161-
/// A Vec can be mutable. Slices, on the other hand, are read-only objects.
168+
/// A `Vec` can be mutable. Slices, on the other hand, are read-only objects.
162169
/// To get a slice, use "&". Example:
163170
///
164171
/// ```
@@ -175,8 +182,8 @@ use super::range::RangeArgument;
175182
/// ```
176183
///
177184
/// In Rust, it's more common to pass slices as arguments rather than vectors
178-
/// when you just want to provide a read access. The same goes for String and
179-
/// &str.
185+
/// when you just want to provide a read access. The same goes for [`String`] and
186+
/// [`&str`].
180187
///
181188
/// # Capacity and reallocation
182189
///
@@ -191,7 +198,7 @@ use super::range::RangeArgument;
191198
/// with space for 10 more elements. Pushing 10 or fewer elements onto the
192199
/// vector will not change its capacity or cause reallocation to occur. However,
193200
/// if the vector's length is increased to 11, it will have to reallocate, which
194-
/// can be slow. For this reason, it is recommended to use `Vec::with_capacity`
201+
/// can be slow. For this reason, it is recommended to use [`Vec::with_capacity`]
195202
/// whenever possible to specify how big the vector is expected to get.
196203
///
197204
/// # Guarantees
@@ -209,65 +216,83 @@ use super::range::RangeArgument;
209216
/// The pointer will never be null, so this type is null-pointer-optimized.
210217
///
211218
/// However, the pointer may not actually point to allocated memory. In particular,
212-
/// if you construct a Vec with capacity 0 via `Vec::new()`, `vec![]`,
213-
/// `Vec::with_capacity(0)`, or by calling `shrink_to_fit()` on an empty Vec, it
214-
/// will not allocate memory. Similarly, if you store zero-sized types inside
215-
/// a Vec, it will not allocate space for them. *Note that in this case the
216-
/// Vec may not report a `capacity()` of 0*. Vec will allocate if and only
217-
/// if `mem::size_of::<T>() * capacity() > 0`. In general, Vec's allocation
219+
/// if you construct a Vec with capacity 0 via [`Vec::new()`], [`vec![]`][`vec!`],
220+
/// [`Vec::with_capacity(0)`][`Vec::with_capacity`], or by calling [`shrink_to_fit()`]
221+
/// on an empty Vec, it will not allocate memory. Similarly, if you store zero-sized
222+
/// types inside a `Vec`, it will not allocate space for them. *Note that in this case
223+
/// the `Vec` may not report a [`capacity()`] of 0*. Vec will allocate if and only
224+
/// if [`mem::size_of::<T>()`]` * capacity() > 0`. In general, `Vec`'s allocation
218225
/// details are subtle enough that it is strongly recommended that you only
219226
/// free memory allocated by a Vec by creating a new Vec and dropping it.
220227
///
221-
/// If a Vec *has* allocated memory, then the memory it points to is on the heap
228+
/// If a `Vec` *has* allocated memory, then the memory it points to is on the heap
222229
/// (as defined by the allocator Rust is configured to use by default), and its
223-
/// pointer points to `len()` initialized elements in order (what you would see
224-
/// if you coerced it to a slice), followed by `capacity() - len()` logically
225-
/// uninitialized elements.
230+
/// pointer points to [`len()`] initialized elements in order (what you would see
231+
/// if you coerced it to a slice), followed by `[capacity()][`capacity()`] -
232+
/// [len()][`len()`]` logically uninitialized elements.
226233
///
227-
/// Vec will never perform a "small optimization" where elements are actually
234+
/// `Vec` will never perform a "small optimization" where elements are actually
228235
/// stored on the stack for two reasons:
229236
///
230237
/// * It would make it more difficult for unsafe code to correctly manipulate
231-
/// a Vec. The contents of a Vec wouldn't have a stable address if it were
232-
/// only moved, and it would be more difficult to determine if a Vec had
238+
/// a `Vec`. The contents of a `Vec` wouldn't have a stable address if it were
239+
/// only moved, and it would be more difficult to determine if a `Vec` had
233240
/// actually allocated memory.
234241
///
235242
/// * It would penalize the general case, incurring an additional branch
236243
/// on every access.
237244
///
238-
/// Vec will never automatically shrink itself, even if completely empty. This
239-
/// ensures no unnecessary allocations or deallocations occur. Emptying a Vec
240-
/// and then filling it back up to the same `len()` should incur no calls to
241-
/// the allocator. If you wish to free up unused memory, use `shrink_to_fit`.
245+
/// `Vec` will never automatically shrink itself, even if completely empty. This
246+
/// ensures no unnecessary allocations or deallocations occur. Emptying a `Vec`
247+
/// and then filling it back up to the same [`len()`] should incur no calls to
248+
/// the allocator. If you wish to free up unused memory, use
249+
/// [`shrink_to_fit`][`shrink_to_fit()`].
242250
///
243-
/// `push` and `insert` will never (re)allocate if the reported capacity is
244-
/// sufficient. `push` and `insert` *will* (re)allocate if `len() == capacity()`.
245-
/// That is, the reported capacity is completely accurate, and can be relied on.
246-
/// It can even be used to manually free the memory allocated by a Vec if
247-
/// desired. Bulk insertion methods *may* reallocate, even when not necessary.
251+
/// [`push`] and [`insert`] will never (re)allocate if the reported capacity is
252+
/// sufficient. [`push`] and [`insert`] *will* (re)allocate if `[len()][`len()`]
253+
/// == [capacity()][`capacity()`]`. That is, the reported capacity is completely
254+
/// accurate, and can be relied on. It can even be used to manually free the memory
255+
/// allocated by a `Vec` if desired. Bulk insertion methods *may* reallocate, even
256+
/// when not necessary.
248257
///
249-
/// Vec does not guarantee any particular growth strategy when reallocating
250-
/// when full, nor when `reserve` is called. The current strategy is basic
258+
/// `Vec` does not guarantee any particular growth strategy when reallocating
259+
/// when full, nor when [`reserve`] is called. The current strategy is basic
251260
/// and it may prove desirable to use a non-constant growth factor. Whatever
252-
/// strategy is used will of course guarantee `O(1)` amortized `push`.
261+
/// strategy is used will of course guarantee `O(1)` amortized [`push`].
253262
///
254-
/// `vec![x; n]`, `vec![a, b, c, d]`, and `Vec::with_capacity(n)`, will all
255-
/// produce a Vec with exactly the requested capacity. If `len() == capacity()`,
256-
/// (as is the case for the `vec!` macro), then a `Vec<T>` can be converted
257-
/// to and from a `Box<[T]>` without reallocating or moving the elements.
263+
/// `vec![x; n]`, `vec![a, b, c, d]`, and
264+
/// [`Vec::with_capacity(n)`][`Vec::with_capacity`], will all
265+
/// produce a `Vec` with exactly the requested capacity. If `[len()][`len()`] ==
266+
/// [capacity()][`capacity()`]`, (as is the case for the [`vec!`] macro), then a
267+
/// `Vec<T>` can be converted to and from a [`Box<[T]>`] without reallocating or
268+
/// moving the elements.
258269
///
259-
/// Vec will not specifically overwrite any data that is removed from it,
270+
/// `Vec` will not specifically overwrite any data that is removed from it,
260271
/// but also won't specifically preserve it. Its uninitialized memory is
261272
/// scratch space that it may use however it wants. It will generally just do
262273
/// whatever is most efficient or otherwise easy to implement. Do not rely on
263-
/// removed data to be erased for security purposes. Even if you drop a Vec, its
264-
/// buffer may simply be reused by another Vec. Even if you zero a Vec's memory
274+
/// removed data to be erased for security purposes. Even if you drop a `Vec`, its
275+
/// buffer may simply be reused by another `Vec`. Even if you zero a `Vec`'s memory
265276
/// first, that may not actually happen because the optimizer does not consider
266277
/// this a side-effect that must be preserved.
267278
///
268-
/// Vec does not currently guarantee the order in which elements are dropped
279+
/// `Vec` does not currently guarantee the order in which elements are dropped
269280
/// (the order has changed in the past, and may change again).
270281
///
282+
/// [`vec!`]: ../../std/macro.vec.html
283+
/// [`Index`]: ../../std/ops/trait.Index.html
284+
/// [`String`]: ../../std/string/struct.String.html
285+
/// [`&str`]: ../../std/primitive.str.html
286+
/// [`Vec::with_capacity`]: ../../std/vec/struct.Vec.html#method.with_capacity
287+
/// [`Vec::new()`]: ../../std/vec/struct.Vec.html#method.new
288+
/// [`shrink_to_fit()`]: ../../std/vec/struct.Vec.html#method.shrink_to_fit
289+
/// [`capacity()`]: ../../std/vec/struct.Vec.html#method.capacity
290+
/// [`mem::size_of::<T>()`]: ../../std/mem/fn.size_of.html
291+
/// [`len()`]: ../../std/vec/struct.Vec.html#method.len
292+
/// [`push`]: ../../std/vec/struct.Vec.html#method.push
293+
/// [`insert`]: ../../std/vec/struct.Vec.html#method.insert
294+
/// [`reserve`]: ../../std/vec/struct.Vec.html#method.reserve
295+
/// [`Box<[T]>`]: ../../std/boxed/struct.Box.html
271296
#[stable(feature = "rust1", since = "1.0.0")]
272297
pub struct Vec<T> {
273298
buf: RawVec<T>,
@@ -340,7 +365,7 @@ impl<T> Vec<T> {
340365
/// This is highly unsafe, due to the number of invariants that aren't
341366
/// checked:
342367
///
343-
/// * `ptr` needs to have been previously allocated via `String`/`Vec<T>`
368+
/// * `ptr` needs to have been previously allocated via [`String`]/`Vec<T>`
344369
/// (at least, it's highly likely to be incorrect if it wasn't).
345370
/// * `length` needs to be less than or equal to `capacity`.
346371
/// * `capacity` needs to be the capacity that the pointer was allocated with.
@@ -354,6 +379,8 @@ impl<T> Vec<T> {
354379
/// that nothing else uses the pointer after calling this
355380
/// function.
356381
///
382+
/// [`String`]: ../../std/string/struct.String.html
383+
///
357384
/// # Examples
358385
///
359386
/// ```
@@ -470,11 +497,15 @@ impl<T> Vec<T> {
470497
self.buf.shrink_to_fit(self.len);
471498
}
472499

473-
/// Converts the vector into Box<[T]>.
500+
/// Converts the vector into [`Box<[T]>`].
474501
///
475502
/// Note that this will drop any excess capacity. Calling this and
476-
/// converting back to a vector with `into_vec()` is equivalent to calling
477-
/// `shrink_to_fit()`.
503+
/// converting back to a vector with [`into_vec()`] is equivalent to calling
504+
/// [`shrink_to_fit()`].
505+
///
506+
/// [`Box<[T]>`]: ../../std/boxed/struct.Box.html
507+
/// [`into_vec()`]: ../../std/primitive.slice.html#method.into_vec
508+
/// [`shrink_to_fit()`]: #method.shrink_to_fit
478509
///
479510
/// # Examples
480511
///
@@ -673,7 +704,7 @@ impl<T> Vec<T> {
673704
///
674705
/// # Panics
675706
///
676-
/// Panics if `index` is greater than the vector's length.
707+
/// Panics if `index` is out of bounds.
677708
///
678709
/// # Examples
679710
///
@@ -933,9 +964,11 @@ impl<T> Vec<T> {
933964
}
934965
}
935966

936-
/// Removes the last element from a vector and returns it, or `None` if it
967+
/// Removes the last element from a vector and returns it, or [`None`] if it
937968
/// is empty.
938969
///
970+
/// [`None`]: ../../std/option/enum.Option.html#variant.None
971+
///
939972
/// # Examples
940973
///
941974
/// ```

0 commit comments

Comments
 (0)