Skip to content

Commit 1f34f50

Browse files
committed
auto merge of #11127 : huonw/rust/vec-docs, r=alexcrichton
2 parents 9477c49 + b29e548 commit 1f34f50

File tree

1 file changed

+115
-27
lines changed

1 file changed

+115
-27
lines changed

src/libstd/vec.rs

Lines changed: 115 additions & 27 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,20 +2255,48 @@ 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

2218-
/**
2219-
* Unchecked vector index assignment. Does not drop the
2220-
* old value and hence is only suitable when the vector
2221-
* is newly allocated.
2222-
*/
2281+
/// Unchecked vector index assignment. Does not drop the
2282+
/// old value and hence is only suitable when the vector
2283+
/// is newly allocated.
2284+
///
2285+
/// # Example
2286+
///
2287+
/// ```rust
2288+
/// let mut v = [~"foo", ~"bar"];
2289+
///
2290+
/// // memory leak! `~"bar"` is not deallocated.
2291+
/// unsafe { v.init_elem(1, ~"baz"); }
2292+
/// ```
22232293
unsafe fn init_elem(self, i: uint, val: T);
22242294

2225-
/// Copies data from `src` to `self`.
2295+
/// Copies raw bytes from `src` to `self`.
22262296
///
2227-
/// `self` and `src` must not overlap. Fails if `self` is
2228-
/// shorter than `src`.
2297+
/// This does not run destructors on the overwritten elements, and
2298+
/// ignores move semantics. `self` and `src` must not
2299+
/// overlap. Fails if `self` is shorter than `src`.
22292300
unsafe fn copy_memory(self, src: &[T]);
22302301
}
22312302

@@ -2370,8 +2441,25 @@ impl<'a,T> MutableVector<'a, T> for &'a mut [T] {
23702441

23712442
/// Trait for &[T] where T is Cloneable
23722443
pub trait MutableCloneableVector<T> {
2373-
/// Copies as many elements from `src` as it can into `self`
2374-
/// (the shorter of self.len() and src.len()). Returns the number of elements copied.
2444+
/// Copies as many elements from `src` as it can into `self` (the
2445+
/// shorter of `self.len()` and `src.len()`). Returns the number
2446+
/// of elements copied.
2447+
///
2448+
/// # Example
2449+
///
2450+
/// ```rust
2451+
/// use std::vec::MutableCloneableVector;
2452+
///
2453+
/// let mut dst = [0, 0, 0];
2454+
/// let src = [1, 2];
2455+
///
2456+
/// assert_eq!(dst.copy_from(src), 2);
2457+
/// assert_eq!(dst, [1, 2, 0]);
2458+
///
2459+
/// let src2 = [3, 4, 5, 6];
2460+
/// assert_eq!(dst.copy_from(src2), 3);
2461+
/// assert_eq!(dst, [3, 4, 5]);
2462+
/// ```
23752463
fn copy_from(self, &[T]) -> uint;
23762464
}
23772465

@@ -2390,7 +2478,7 @@ impl<'a, T:Clone> MutableCloneableVector<T> for &'a mut [T] {
23902478
pub trait MutableTotalOrdVector<T> {
23912479
/// Sort the vector, in place.
23922480
///
2393-
/// This is equivalent to `self.sort_by(std::vec::SortForward)`.
2481+
/// This is equivalent to `self.sort_by(|a, b| a.cmp(b))`.
23942482
///
23952483
/// # Example
23962484
///

0 commit comments

Comments
 (0)