about summary refs log tree commit diff
path: root/src/libcollections
diff options
context:
space:
mode:
authorJake Goulding <jake.goulding@gmail.com>2015-03-14 19:34:21 -0400
committerJake Goulding <jake.goulding@gmail.com>2015-03-19 19:25:22 -0400
commit6a5148bda1364bd46607a3c4ebdcfb0f408e0850 (patch)
tree7b1644c37465b8e6367eefb2d007c70dea9c951a /src/libcollections
parent7f53b943f94b338e4c5401f1ce9efbe7da92b0c5 (diff)
downloadrust-6a5148bda1364bd46607a3c4ebdcfb0f408e0850.tar.gz
rust-6a5148bda1364bd46607a3c4ebdcfb0f408e0850.zip
Introduce rsplit
Diffstat (limited to 'src/libcollections')
-rw-r--r--src/libcollections/str.rs32
1 files changed, 30 insertions, 2 deletions
diff --git a/src/libcollections/str.rs b/src/libcollections/str.rs
index 3a289e4ef37..6379155800b 100644
--- a/src/libcollections/str.rs
+++ b/src/libcollections/str.rs
@@ -74,8 +74,8 @@ use slice::SliceConcatExt;
 
 pub use core::str::{FromStr, Utf8Error, Str};
 pub use core::str::{Lines, LinesAny, MatchIndices, SplitStr, CharRange};
-pub use core::str::{Split, SplitTerminator};
-pub use core::str::{SplitN, RSplitN};
+pub use core::str::{Split, SplitTerminator, SplitN};
+pub use core::str::{RSplit, RSplitN};
 pub use core::str::{from_utf8, CharEq, Chars, CharIndices, Bytes};
 pub use core::str::{from_utf8_unchecked, from_c_str, ParseBoolError};
 pub use unicode::str::{Words, Graphemes, GraphemeIndices};
@@ -699,6 +699,34 @@ impl str {
         core_str::StrExt::split_terminator(&self[..], pat)
     }
 
+    /// An iterator over substrings of `self`, separated by a pattern,
+    /// starting from the end of the string.
+    ///
+    /// # Examples
+    ///
+    /// Simple patterns:
+    ///
+    /// ```
+    /// let v: Vec<&str> = "Mary had a little lamb".rsplit(' ').collect();
+    /// assert_eq!(v, ["lamb", "little", "a", "had", "Mary"]);
+    ///
+    /// let v: Vec<&str> = "lion::tiger::leopard".rsplit("::").collect();
+    /// assert_eq!(v, ["leopard", "tiger", "lion"]);
+    /// ```
+    ///
+    /// More complex patterns with a lambda:
+    ///
+    /// ```
+    /// let v: Vec<&str> = "abc1def2ghi".rsplit(|c: char| c.is_numeric()).collect();
+    /// assert_eq!(v, ["ghi", "def", "abc"]);
+    /// ```
+    #[stable(feature = "rust1", since = "1.0.0")]
+    pub fn rsplit<'a, P: Pattern<'a>>(&'a self, pat: P) -> RSplit<'a, P>
+        where P::Searcher: ReverseSearcher<'a>
+    {
+        core_str::StrExt::rsplit(&self[..], pat)
+    }
+
     /// An iterator over substrings of `self`, separated by characters matched by a pattern,
     /// starting from the end of the string.
     ///