Skip to content

Commit

Permalink
Rollup merge of rust-lang#21625 - carols10cents:sliceext-examples, r=…
Browse files Browse the repository at this point in the history
…alexcrichton

Hi! I added some examples to some stable 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! ❤️
  • Loading branch information
barosl committed Jan 27, 2015
2 parents ec3cbeb + ebd2d8d commit 0d3dee2
Showing 1 changed file with 105 additions and 4 deletions.
109 changes: 105 additions & 4 deletions src/libcollections/slice.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
Expand Down Expand Up @@ -174,7 +174,7 @@ pub trait SliceExt {
fn slice(&self, start: uint, end: uint) -> &[Self::Item];

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

/// Deprecated: use `&s[..end]` notation instead.
Expand All @@ -188,22 +188,55 @@ pub trait SliceExt {
/// indices from `[mid, len)` (excluding the index `len` itself).
///
/// Panics if `mid > len`.
///
/// # Examples
///
/// ```
/// let v = [10, 40, 30, 20, 50];
/// let (v1, v2) = v.split_at(2);
/// assert_eq!([10, 40], v1);
/// assert_eq!([30, 20, 50], v2);
/// ```
#[stable]
fn split_at(&self, mid: uint) -> (&[Self::Item], &[Self::Item]);

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

/// Returns an iterator over subslices separated by elements that match
/// `pred`. The matched element is not contained in the subslices.
///
/// # Examples
///
/// Print the slice split by numbers divisible by 3 (i.e. `[10, 40]`,
/// `[20]`, `[50]`):
///
/// ```
/// let v = [10, 40, 30, 20, 60, 50];
/// for group in v.split(|num| *num % 3 == 0) {
/// println!("{:?}", group);
/// }
/// ```
#[stable]
fn split<F>(&self, pred: F) -> Split<Self::Item, F>
where F: FnMut(&Self::Item) -> bool;

/// Returns an iterator over subslices separated by elements that match
/// `pred`, limited to splitting at most `n` times. The matched element is
/// not contained in the subslices.
///
/// # Examples
///
/// Print the slice split once by numbers divisible by 3 (i.e. `[10, 40]`,
/// `[20, 60, 50]`):
///
/// ```
/// let v = [10, 40, 30, 20, 60, 50];
/// for group in v.splitn(1, |num| *num % 3 == 0) {
/// println!("{:?}", group);
/// }
/// ```
#[stable]
fn splitn<F>(&self, n: uint, pred: F) -> SplitN<Self::Item, F>
where F: FnMut(&Self::Item) -> bool;
Expand All @@ -212,6 +245,18 @@ pub trait SliceExt {
/// `pred` limited to splitting at most `n` times. This starts at the end of
/// the slice and works backwards. The matched element is not contained in
/// the subslices.
///
/// # Examples
///
/// Print the slice split once, starting from the end, by numbers divisible
/// by 3 (i.e. `[50]`, `[10, 40, 30, 20]`):
///
/// ```
/// let v = [10, 40, 30, 20, 60, 50];
/// for group in v.rsplitn(1, |num| *num % 3 == 0) {
/// println!("{:?}", group);
/// }
/// ```
#[stable]
fn rsplitn<F>(&self, n: uint, pred: F) -> RSplitN<Self::Item, F>
where F: FnMut(&Self::Item) -> bool;
Expand Down Expand Up @@ -263,10 +308,28 @@ pub trait SliceExt {

/// Returns the element of a slice at the given index, or `None` if the
/// index is out of bounds.
///
/// # Examples
///
/// ```
/// let v = [10, 40, 30];
/// assert_eq!(Some(&40), v.get(1));
/// assert_eq!(None, v.get(3));
/// ```
#[stable]
fn get(&self, index: uint) -> Option<&Self::Item>;

/// Returns the first element of a slice, or `None` if it is empty.
///
/// # Examples
///
/// ```
/// let v = [10, 40, 30];
/// assert_eq!(Some(&10), v.first());
///
/// let w: &[i32] = &[];
/// assert_eq!(None, w.first());
/// ```
#[stable]
fn first(&self) -> Option<&Self::Item>;

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

/// Returns the last element of a slice, or `None` if it is empty.
///
/// # Examples
///
/// ```
/// let v = [10, 40, 30];
/// assert_eq!(Some(&30), v.last());
///
/// let w: &[i32] = &[];
/// assert_eq!(None, w.last());
/// ```
#[stable]
fn last(&self) -> Option<&Self::Item>;

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

/// Return true if the slice contains an element with the given value.
/// Returns true if the slice contains an element with the given value.
///
/// # Examples
///
/// ```
/// let v = [10, 40, 30];
/// assert!(v.contains(&30));
/// assert!(!v.contains(&50));
/// ```
#[stable]
fn contains(&self, x: &Self::Item) -> bool where Self::Item: PartialEq;

/// Returns true if `needle` is a prefix of the slice.
///
/// # Examples
///
/// ```
/// let v = [10, 40, 30];
/// assert!(v.starts_with(&[10]));
/// assert!(v.starts_with(&[10, 40]));
/// assert!(!v.starts_with(&[50]));
/// assert!(!v.starts_with(&[10, 50]));
/// ```
#[stable]
fn starts_with(&self, needle: &[Self::Item]) -> bool where Self::Item: PartialEq;

/// Returns true if `needle` is a suffix of the slice.
///
/// # Examples
///
/// ```
/// let v = [10, 40, 30];
/// assert!(v.ends_with(&[30]));
/// assert!(v.ends_with(&[40, 30]));
/// assert!(!v.ends_with(&[50]));
/// assert!(!v.ends_with(&[50, 30]));
/// ```
#[stable]
fn ends_with(&self, needle: &[Self::Item]) -> bool where Self::Item: PartialEq;

Expand Down

0 comments on commit 0d3dee2

Please sign in to comment.