Skip to content

Commit 335e8af

Browse files
committedJan 27, 2015
Rollup merge of rust-lang#21625 - carols10cents:sliceext-examples, r=alexcrichton
Hi! I added some examples to some SliceExt methods that didn't have any. I'm looking forward to feedback and I'm happy to change anything-- it looks like the doc conventions are still a bit in flux, based on the discussions going on in [rfc 505](rust-lang/rfcs#505). I was most unsure about examples for methods that return iterators over slices... I wanted to use asserts on the result of calling `.next()` like in [this permutations example](https://github.com/carols10cents/rust/blob/804c1446b3b0afd84851339d8ee2be1dca8f7713/src/libcollections/slice.rs#L608-L617), but then it gets all cluttered up with lifetime stuff... so I went with iterating and printing and mentioning what the expected printed output is like in [this chunks example](https://github.com/carols10cents/rust/blob/804c1446b3b0afd84851339d8ee2be1dca8f7713/src/libcollections/slice.rs#L297-L304)... any ideas for the best ways to do this are appreciated. Thank you! ❤️
2 parents 4af4b37 + ebd2d8d commit 335e8af

File tree

1 file changed

+105
-4
lines changed

1 file changed

+105
-4
lines changed
 

‎src/libcollections/slice.rs

+105-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -174,7 +174,7 @@ pub trait SliceExt {
174174
fn slice(&self, start: uint, end: uint) -> &[Self::Item];
175175

176176
/// Deprecated: use `&s[start..]` notation instead.
177-
#[deprecated = "use &s[start..] isntead"]
177+
#[deprecated = "use &s[start..] instead"]
178178
fn slice_from(&self, start: uint) -> &[Self::Item];
179179

180180
/// Deprecated: use `&s[..end]` notation instead.
@@ -188,22 +188,55 @@ pub trait SliceExt {
188188
/// indices from `[mid, len)` (excluding the index `len` itself).
189189
///
190190
/// 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+
/// ```
191200
#[stable]
192201
fn split_at(&self, mid: uint) -> (&[Self::Item], &[Self::Item]);
193202

194-
/// Returns an iterator over the slice
203+
/// Returns an iterator over the slice.
195204
#[stable]
196205
fn iter(&self) -> Iter<Self::Item>;
197206

198207
/// Returns an iterator over subslices separated by elements that match
199208
/// `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+
/// ```
200221
#[stable]
201222
fn split<F>(&self, pred: F) -> Split<Self::Item, F>
202223
where F: FnMut(&Self::Item) -> bool;
203224

204225
/// Returns an iterator over subslices separated by elements that match
205226
/// `pred`, limited to splitting at most `n` times. The matched element is
206227
/// 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+
/// ```
207240
#[stable]
208241
fn splitn<F>(&self, n: uint, pred: F) -> SplitN<Self::Item, F>
209242
where F: FnMut(&Self::Item) -> bool;
@@ -212,6 +245,18 @@ pub trait SliceExt {
212245
/// `pred` limited to splitting at most `n` times. This starts at the end of
213246
/// the slice and works backwards. The matched element is not contained in
214247
/// 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+
/// ```
215260
#[stable]
216261
fn rsplitn<F>(&self, n: uint, pred: F) -> RSplitN<Self::Item, F>
217262
where F: FnMut(&Self::Item) -> bool;
@@ -263,10 +308,28 @@ pub trait SliceExt {
263308

264309
/// Returns the element of a slice at the given index, or `None` if the
265310
/// 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+
/// ```
266319
#[stable]
267320
fn get(&self, index: uint) -> Option<&Self::Item>;
268321

269322
/// 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+
/// ```
270333
#[stable]
271334
fn first(&self) -> Option<&Self::Item>;
272335

@@ -279,6 +342,16 @@ pub trait SliceExt {
279342
fn init(&self) -> &[Self::Item];
280343

281344
/// 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+
/// ```
282355
#[stable]
283356
fn last(&self) -> Option<&Self::Item>;
284357

@@ -658,15 +731,43 @@ pub trait SliceExt {
658731
#[unstable]
659732
fn rposition_elem(&self, t: &Self::Item) -> Option<uint> where Self::Item: PartialEq;
660733

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+
/// ```
662743
#[stable]
663744
fn contains(&self, x: &Self::Item) -> bool where Self::Item: PartialEq;
664745

665746
/// 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+
/// ```
666757
#[stable]
667758
fn starts_with(&self, needle: &[Self::Item]) -> bool where Self::Item: PartialEq;
668759

669760
/// 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+
/// ```
670771
#[stable]
671772
fn ends_with(&self, needle: &[Self::Item]) -> bool where Self::Item: PartialEq;
672773

0 commit comments

Comments
 (0)
Please sign in to comment.