@@ -2143,26 +2143,69 @@ pub trait MutableVector<'a, T> {
2143
2143
*/
2144
2144
fn mut_pop_ref ( & mut self ) -> & ' a mut T ;
2145
2145
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
+ /// ```
2154
2162
fn swap ( self , a : uint , b : uint ) ;
2155
2163
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
+ /// ```
2162
2197
fn mut_split_at ( self , mid : uint ) -> ( & ' a mut [ T ] ,
2163
2198
& ' a mut [ T ] ) ;
2164
2199
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
+ /// ```
2166
2209
fn reverse ( self ) ;
2167
2210
2168
2211
/// Sort the vector, in place, using `compare` to compare
@@ -2212,20 +2255,48 @@ pub trait MutableVector<'a, T> {
2212
2255
#[ inline]
2213
2256
fn as_mut_ptr ( self ) -> * mut T ;
2214
2257
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
+ /// ```
2216
2279
unsafe fn unsafe_set ( self , index : uint , val : T ) ;
2217
2280
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
+ /// ```
2223
2293
unsafe fn init_elem ( self , i : uint , val : T ) ;
2224
2294
2225
- /// Copies data from `src` to `self`.
2295
+ /// Copies raw bytes from `src` to `self`.
2226
2296
///
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`.
2229
2300
unsafe fn copy_memory ( self , src : & [ T ] ) ;
2230
2301
}
2231
2302
@@ -2370,8 +2441,25 @@ impl<'a,T> MutableVector<'a, T> for &'a mut [T] {
2370
2441
2371
2442
/// Trait for &[T] where T is Cloneable
2372
2443
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
+ /// ```
2375
2463
fn copy_from ( self , & [ T ] ) -> uint ;
2376
2464
}
2377
2465
@@ -2390,7 +2478,7 @@ impl<'a, T:Clone> MutableCloneableVector<T> for &'a mut [T] {
2390
2478
pub trait MutableTotalOrdVector < T > {
2391
2479
/// Sort the vector, in place.
2392
2480
///
2393
- /// This is equivalent to `self.sort_by(std::vec::SortForward )`.
2481
+ /// This is equivalent to `self.sort_by(|a, b| a.cmp(b) )`.
2394
2482
///
2395
2483
/// # Example
2396
2484
///
0 commit comments