Skip to content

Commit b29e548

Browse files
committed
std::vec: clarify & examplify more docs.
1 parent cc4e707 commit b29e548

File tree

1 file changed

+79
-16
lines changed

1 file changed

+79
-16
lines changed

src/libstd/vec.rs

Lines changed: 79 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2143,26 +2143,69 @@ pub trait MutableVector<'a, T> {
21432143
*/
21442144
fn mut_pop_ref(&mut self) -> &'a mut T;
21452145

2146-
/**
2147-
* Swaps two elements in a vector
2148-
*
2149-
* # Arguments
2150-
*
2151-
* * a - The index of the first element
2152-
* * b - The index of the second element
2153-
*/
2146+
/// Swaps two elements in a vector.
2147+
///
2148+
/// Fails if `a` or `b` are out of bounds.
2149+
///
2150+
/// # Arguments
2151+
///
2152+
/// * a - The index of the first element
2153+
/// * b - The index of the second element
2154+
///
2155+
/// # Example
2156+
///
2157+
/// ```rust
2158+
/// let mut v = ["a", "b", "c", "d"];
2159+
/// v.swap(1, 3);
2160+
/// assert_eq!(v, ["a", "d", "c", "b"]);
2161+
/// ```
21542162
fn swap(self, a: uint, b: uint);
21552163

2156-
/**
2157-
* Divides one `&mut` into two. The first will
2158-
* contain all indices from `0..mid` (excluding the index `mid`
2159-
* itself) and the second will contain all indices from
2160-
* `mid..len` (excluding the index `len` itself).
2161-
*/
2164+
2165+
/// Divides one `&mut` into two at an index.
2166+
///
2167+
/// The first will contain all indices from `[0, mid)` (excluding
2168+
/// the index `mid` itself) and the second will contain all
2169+
/// indices from `[mid, len)` (excluding the index `len` itself).
2170+
///
2171+
/// Fails if `mid > len`.
2172+
///
2173+
/// # Example
2174+
///
2175+
/// ```rust
2176+
/// let mut v = [1, 2, 3, 4, 5, 6];
2177+
///
2178+
/// // scoped to restrict the lifetime of the borrows
2179+
/// {
2180+
/// let (left, right) = v.mut_split_at(0);
2181+
/// assert_eq!(left, &mut []);
2182+
/// assert_eq!(right, &mut [1, 2, 3, 4, 5, 6]);
2183+
/// }
2184+
///
2185+
/// {
2186+
/// let (left, right) = v.mut_split_at(2);
2187+
/// assert_eq!(left, &mut [1, 2]);
2188+
/// assert_eq!(right, &mut [3, 4, 5, 6]);
2189+
/// }
2190+
///
2191+
/// {
2192+
/// let (left, right) = v.mut_split_at(6);
2193+
/// assert_eq!(left, &mut [1, 2, 3, 4, 5, 6]);
2194+
/// assert_eq!(right, &mut []);
2195+
/// }
2196+
/// ```
21622197
fn mut_split_at(self, mid: uint) -> (&'a mut [T],
21632198
&'a mut [T]);
21642199

2165-
/// Reverse the order of elements in a vector, in place
2200+
/// Reverse the order of elements in a vector, in place.
2201+
///
2202+
/// # Example
2203+
///
2204+
/// ```rust
2205+
/// let mut v = [1, 2, 3];
2206+
/// v.reverse();
2207+
/// assert_eq!(v, [3, 2, 1]);
2208+
/// ```
21662209
fn reverse(self);
21672210

21682211
/// Sort the vector, in place, using `compare` to compare
@@ -2212,7 +2255,27 @@ pub trait MutableVector<'a, T> {
22122255
#[inline]
22132256
fn as_mut_ptr(self) -> *mut T;
22142257

2215-
/// Unsafely sets the element in index to the value
2258+
/// Unsafely sets the element in index to the value.
2259+
///
2260+
/// This performs no bounds checks, and it is undefined behaviour
2261+
/// if `index` is larger than the length of `self`. However, it
2262+
/// does run the destructor at `index`. It is equivalent to
2263+
/// `self[index] = val`.
2264+
///
2265+
/// # Example
2266+
///
2267+
/// ```rust
2268+
/// let mut v = ~[~"foo", ~"bar", ~"baz"];
2269+
///
2270+
/// unsafe {
2271+
/// // `~"baz"` is deallocated.
2272+
/// v.unsafe_set(2, ~"qux");
2273+
///
2274+
/// // Out of bounds: could cause a crash, or overwriting
2275+
/// // other data, or something else.
2276+
/// // v.unsafe_set(10, ~"oops");
2277+
/// }
2278+
/// ```
22162279
unsafe fn unsafe_set(self, index: uint, val: T);
22172280

22182281
/// Unchecked vector index assignment. Does not drop the

0 commit comments

Comments
 (0)