std: Changing the meaning of the count to splitn
This commit is an implementation of [RFC 979][rfc] which changes the meaning of the count parameter to the `splitn` function on strings and slices. The parameter now means the number of items that are returned from the iterator, not the number of splits that are made. [rfc]: https://github.com/rust-lang/rfcs/pull/979 Closes #23911 [breaking-change]
This commit is contained in:
parent
d528aa9960
commit
e98dce3e00
14 changed files with 88 additions and 69 deletions
|
|
@ -328,9 +328,12 @@ impl<T> [T] {
|
|||
}
|
||||
|
||||
/// Returns an iterator over subslices separated by elements that match
|
||||
/// `pred`, limited to splitting at most `n` times. The matched element is
|
||||
/// `pred`, limited to returning at most `n` items. The matched element is
|
||||
/// not contained in the subslices.
|
||||
///
|
||||
/// The last element returned, if any, will contain the remainder of the
|
||||
/// slice.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// Print the slice split once by numbers divisible by 3 (i.e. `[10, 40]`,
|
||||
|
|
@ -338,7 +341,7 @@ impl<T> [T] {
|
|||
///
|
||||
/// ```
|
||||
/// let v = [10, 40, 30, 20, 60, 50];
|
||||
/// for group in v.splitn(1, |num| *num % 3 == 0) {
|
||||
/// for group in v.splitn(2, |num| *num % 3 == 0) {
|
||||
/// println!("{:?}", group);
|
||||
/// }
|
||||
/// ```
|
||||
|
|
@ -349,10 +352,13 @@ impl<T> [T] {
|
|||
}
|
||||
|
||||
/// Returns an iterator over subslices separated by elements that match
|
||||
/// `pred` limited to splitting at most `n` times. This starts at the end of
|
||||
/// `pred` limited to returning at most `n` items. This starts at the end of
|
||||
/// the slice and works backwards. The matched element is not contained in
|
||||
/// the subslices.
|
||||
///
|
||||
/// The last element returned, if any, will contain the remainder of the
|
||||
/// slice.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// Print the slice split once, starting from the end, by numbers divisible
|
||||
|
|
@ -360,7 +366,7 @@ impl<T> [T] {
|
|||
///
|
||||
/// ```
|
||||
/// let v = [10, 40, 30, 20, 60, 50];
|
||||
/// for group in v.rsplitn(1, |num| *num % 3 == 0) {
|
||||
/// for group in v.rsplitn(2, |num| *num % 3 == 0) {
|
||||
/// println!("{:?}", group);
|
||||
/// }
|
||||
/// ```
|
||||
|
|
@ -626,8 +632,11 @@ impl<T> [T] {
|
|||
}
|
||||
|
||||
/// Returns an iterator over subslices separated by elements that match
|
||||
/// `pred`, limited to splitting at most `n` times. The matched element is
|
||||
/// `pred`, limited to returning at most `n` items. The matched element is
|
||||
/// not contained in the subslices.
|
||||
///
|
||||
/// The last element returned, if any, will contain the remainder of the
|
||||
/// slice.
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
#[inline]
|
||||
pub fn splitn_mut<F>(&mut self, n: usize, pred: F) -> SplitNMut<T, F>
|
||||
|
|
@ -636,9 +645,12 @@ impl<T> [T] {
|
|||
}
|
||||
|
||||
/// Returns an iterator over subslices separated by elements that match
|
||||
/// `pred` limited to splitting at most `n` times. This starts at the end of
|
||||
/// `pred` limited to returning at most `n` items. This starts at the end of
|
||||
/// the slice and works backwards. The matched element is not contained in
|
||||
/// the subslices.
|
||||
///
|
||||
/// The last element returned, if any, will contain the remainder of the
|
||||
/// slice.
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
#[inline]
|
||||
pub fn rsplitn_mut<F>(&mut self, n: usize, pred: F) -> RSplitNMut<T, F>
|
||||
|
|
|
|||
|
|
@ -610,24 +610,27 @@ impl str {
|
|||
core_str::StrExt::split(&self[..], pat)
|
||||
}
|
||||
|
||||
/// An iterator over substrings of `self`, separated by characters matched by a pattern,
|
||||
/// restricted to splitting at most `count` times.
|
||||
/// An iterator over substrings of `self`, separated by characters matched
|
||||
/// by a pattern, returning most `count` items.
|
||||
///
|
||||
/// The pattern can be a simple `&str`, or a closure that determines
|
||||
/// the split.
|
||||
///
|
||||
/// The last element returned, if any, will contain the remainder of the
|
||||
/// string.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// Simple `&str` patterns:
|
||||
///
|
||||
/// ```
|
||||
/// let v: Vec<&str> = "Mary had a little lambda".splitn(2, ' ').collect();
|
||||
/// assert_eq!(v, ["Mary", "had", "a little lambda"]);
|
||||
/// assert_eq!(v, ["Mary", "had a little lambda"]);
|
||||
///
|
||||
/// let v: Vec<&str> = "lionXXtigerXleopard".splitn(2, 'X').collect();
|
||||
/// assert_eq!(v, ["lion", "", "tigerXleopard"]);
|
||||
/// assert_eq!(v, ["lion", "XtigerXleopard"]);
|
||||
///
|
||||
/// let v: Vec<&str> = "abcXdef".splitn(0, 'X').collect();
|
||||
/// let v: Vec<&str> = "abcXdef".splitn(1, 'X').collect();
|
||||
/// assert_eq!(v, ["abcXdef"]);
|
||||
///
|
||||
/// let v: Vec<&str> = "".splitn(1, 'X').collect();
|
||||
|
|
@ -637,7 +640,7 @@ impl str {
|
|||
/// More complex patterns with a lambda:
|
||||
///
|
||||
/// ```
|
||||
/// let v: Vec<&str> = "abc1def2ghi".splitn(1, |c: char| c.is_numeric()).collect();
|
||||
/// let v: Vec<&str> = "abc1def2ghi".splitn(2, |c: char| c.is_numeric()).collect();
|
||||
/// assert_eq!(v, ["abc", "def2ghi"]);
|
||||
/// ```
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
|
|
@ -705,25 +708,28 @@ impl str {
|
|||
}
|
||||
|
||||
/// An iterator over substrings of `self`, separated by a pattern,
|
||||
/// starting from the end of the string, restricted to splitting
|
||||
/// at most `count` times.
|
||||
/// starting from the end of the string, restricted to returning
|
||||
/// at most `count` items.
|
||||
///
|
||||
/// The last element returned, if any, will contain the remainder of the
|
||||
/// string.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// Simple patterns:
|
||||
///
|
||||
/// ```
|
||||
/// let v: Vec<&str> = "Mary had a little lamb".rsplitn(2, ' ').collect();
|
||||
/// let v: Vec<&str> = "Mary had a little lamb".rsplitn(3, ' ').collect();
|
||||
/// assert_eq!(v, ["lamb", "little", "Mary had a"]);
|
||||
///
|
||||
/// let v: Vec<&str> = "lion::tiger::leopard".rsplitn(1, "::").collect();
|
||||
/// let v: Vec<&str> = "lion::tiger::leopard".rsplitn(2, "::").collect();
|
||||
/// assert_eq!(v, ["leopard", "lion::tiger"]);
|
||||
/// ```
|
||||
///
|
||||
/// More complex patterns with a lambda:
|
||||
///
|
||||
/// ```
|
||||
/// let v: Vec<&str> = "abc1def2ghi".rsplitn(1, |c: char| c.is_numeric()).collect();
|
||||
/// let v: Vec<&str> = "abc1def2ghi".rsplitn(2, |c: char| c.is_numeric()).collect();
|
||||
/// assert_eq!(v, ["ghi", "abc1def"]);
|
||||
/// ```
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue