1
- // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
1
+ // Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
2
2
// file at the top-level directory of this distribution and at
3
3
// http://rust-lang.org/COPYRIGHT.
4
4
//
@@ -174,7 +174,7 @@ pub trait SliceExt {
174
174
fn slice ( & self , start : uint , end : uint ) -> & [ Self :: Item ] ;
175
175
176
176
/// Deprecated: use `&s[start..]` notation instead.
177
- #[ deprecated = "use &s[start..] isntead " ]
177
+ #[ deprecated = "use &s[start..] instead " ]
178
178
fn slice_from ( & self , start : uint ) -> & [ Self :: Item ] ;
179
179
180
180
/// Deprecated: use `&s[..end]` notation instead.
@@ -188,22 +188,55 @@ pub trait SliceExt {
188
188
/// indices from `[mid, len)` (excluding the index `len` itself).
189
189
///
190
190
/// Panics if `mid > len`.
191
+ ///
192
+ /// # Examples
193
+ ///
194
+ /// ```
195
+ /// let v = [10, 40, 30, 20, 50];
196
+ /// let (v1, v2) = v.split_at(2);
197
+ /// assert_eq!([10, 40], v1);
198
+ /// assert_eq!([30, 20, 50], v2);
199
+ /// ```
191
200
#[ stable]
192
201
fn split_at ( & self , mid : uint ) -> ( & [ Self :: Item ] , & [ Self :: Item ] ) ;
193
202
194
- /// Returns an iterator over the slice
203
+ /// Returns an iterator over the slice.
195
204
#[ stable]
196
205
fn iter ( & self ) -> Iter < Self :: Item > ;
197
206
198
207
/// Returns an iterator over subslices separated by elements that match
199
208
/// `pred`. The matched element is not contained in the subslices.
209
+ ///
210
+ /// # Examples
211
+ ///
212
+ /// Print the slice split by numbers divisible by 3 (i.e. `[10, 40]`,
213
+ /// `[20]`, `[50]`):
214
+ ///
215
+ /// ```
216
+ /// let v = [10, 40, 30, 20, 60, 50];
217
+ /// for group in v.split(|num| *num % 3 == 0) {
218
+ /// println!("{:?}", group);
219
+ /// }
220
+ /// ```
200
221
#[ stable]
201
222
fn split < F > ( & self , pred : F ) -> Split < Self :: Item , F >
202
223
where F : FnMut ( & Self :: Item ) -> bool ;
203
224
204
225
/// Returns an iterator over subslices separated by elements that match
205
226
/// `pred`, limited to splitting at most `n` times. The matched element is
206
227
/// not contained in the subslices.
228
+ ///
229
+ /// # Examples
230
+ ///
231
+ /// Print the slice split once by numbers divisible by 3 (i.e. `[10, 40]`,
232
+ /// `[20, 60, 50]`):
233
+ ///
234
+ /// ```
235
+ /// let v = [10, 40, 30, 20, 60, 50];
236
+ /// for group in v.splitn(1, |num| *num % 3 == 0) {
237
+ /// println!("{:?}", group);
238
+ /// }
239
+ /// ```
207
240
#[ stable]
208
241
fn splitn < F > ( & self , n : uint , pred : F ) -> SplitN < Self :: Item , F >
209
242
where F : FnMut ( & Self :: Item ) -> bool ;
@@ -212,6 +245,18 @@ pub trait SliceExt {
212
245
/// `pred` limited to splitting at most `n` times. This starts at the end of
213
246
/// the slice and works backwards. The matched element is not contained in
214
247
/// the subslices.
248
+ ///
249
+ /// # Examples
250
+ ///
251
+ /// Print the slice split once, starting from the end, by numbers divisible
252
+ /// by 3 (i.e. `[50]`, `[10, 40, 30, 20]`):
253
+ ///
254
+ /// ```
255
+ /// let v = [10, 40, 30, 20, 60, 50];
256
+ /// for group in v.rsplitn(1, |num| *num % 3 == 0) {
257
+ /// println!("{:?}", group);
258
+ /// }
259
+ /// ```
215
260
#[ stable]
216
261
fn rsplitn < F > ( & self , n : uint , pred : F ) -> RSplitN < Self :: Item , F >
217
262
where F : FnMut ( & Self :: Item ) -> bool ;
@@ -263,10 +308,28 @@ pub trait SliceExt {
263
308
264
309
/// Returns the element of a slice at the given index, or `None` if the
265
310
/// index is out of bounds.
311
+ ///
312
+ /// # Examples
313
+ ///
314
+ /// ```
315
+ /// let v = [10, 40, 30];
316
+ /// assert_eq!(Some(&40), v.get(1));
317
+ /// assert_eq!(None, v.get(3));
318
+ /// ```
266
319
#[ stable]
267
320
fn get ( & self , index : uint ) -> Option < & Self :: Item > ;
268
321
269
322
/// Returns the first element of a slice, or `None` if it is empty.
323
+ ///
324
+ /// # Examples
325
+ ///
326
+ /// ```
327
+ /// let v = [10, 40, 30];
328
+ /// assert_eq!(Some(&10), v.first());
329
+ ///
330
+ /// let w: &[i32] = &[];
331
+ /// assert_eq!(None, w.first());
332
+ /// ```
270
333
#[ stable]
271
334
fn first ( & self ) -> Option < & Self :: Item > ;
272
335
@@ -279,6 +342,16 @@ pub trait SliceExt {
279
342
fn init ( & self ) -> & [ Self :: Item ] ;
280
343
281
344
/// Returns the last element of a slice, or `None` if it is empty.
345
+ ///
346
+ /// # Examples
347
+ ///
348
+ /// ```
349
+ /// let v = [10, 40, 30];
350
+ /// assert_eq!(Some(&30), v.last());
351
+ ///
352
+ /// let w: &[i32] = &[];
353
+ /// assert_eq!(None, w.last());
354
+ /// ```
282
355
#[ stable]
283
356
fn last ( & self ) -> Option < & Self :: Item > ;
284
357
@@ -658,15 +731,43 @@ pub trait SliceExt {
658
731
#[ unstable]
659
732
fn rposition_elem ( & self , t : & Self :: Item ) -> Option < uint > where Self :: Item : PartialEq ;
660
733
661
- /// Return true if the slice contains an element with the given value.
734
+ /// Returns true if the slice contains an element with the given value.
735
+ ///
736
+ /// # Examples
737
+ ///
738
+ /// ```
739
+ /// let v = [10, 40, 30];
740
+ /// assert!(v.contains(&30));
741
+ /// assert!(!v.contains(&50));
742
+ /// ```
662
743
#[ stable]
663
744
fn contains ( & self , x : & Self :: Item ) -> bool where Self :: Item : PartialEq ;
664
745
665
746
/// Returns true if `needle` is a prefix of the slice.
747
+ ///
748
+ /// # Examples
749
+ ///
750
+ /// ```
751
+ /// let v = [10, 40, 30];
752
+ /// assert!(v.starts_with(&[10]));
753
+ /// assert!(v.starts_with(&[10, 40]));
754
+ /// assert!(!v.starts_with(&[50]));
755
+ /// assert!(!v.starts_with(&[10, 50]));
756
+ /// ```
666
757
#[ stable]
667
758
fn starts_with ( & self , needle : & [ Self :: Item ] ) -> bool where Self :: Item : PartialEq ;
668
759
669
760
/// Returns true if `needle` is a suffix of the slice.
761
+ ///
762
+ /// # Examples
763
+ ///
764
+ /// ```
765
+ /// let v = [10, 40, 30];
766
+ /// assert!(v.ends_with(&[30]));
767
+ /// assert!(v.ends_with(&[40, 30]));
768
+ /// assert!(!v.ends_with(&[50]));
769
+ /// assert!(!v.ends_with(&[50, 30]));
770
+ /// ```
670
771
#[ stable]
671
772
fn ends_with ( & self , needle : & [ Self :: Item ] ) -> bool where Self :: Item : PartialEq ;
672
773
0 commit comments