]> git.lizzy.rs Git - rust.git/commitdiff
Add as_str() and AsRef to string::Drain.
authorMara Bos <m-ou.se@m-ou.se>
Wed, 9 Sep 2020 15:40:15 +0000 (17:40 +0200)
committerMara Bos <m-ou.se@m-ou.se>
Wed, 9 Sep 2020 15:50:55 +0000 (17:50 +0200)
library/alloc/src/string.rs

index e1724bf3c9a90ce4c67f688784de874384ce1fff..d27dd9c93402c966eb9d458589c8637b40c20879 100644 (file)
@@ -2462,6 +2462,32 @@ fn drop(&mut self) {
     }
 }
 
+impl<'a> Drain<'a> {
+    /// Returns the remaining (sub)string of this iterator as a slice.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// #![feature(string_drain_as_str)]
+    /// let mut s = String::from("abc");
+    /// let mut drain = s.drain(..);
+    /// assert_eq!(drain.as_str(), "abc");
+    /// let _ = drain.next().unwrap();
+    /// assert_eq!(drain.as_str(), "bc");
+    /// ```
+    #[unstable(feature = "string_drain_as_str", issue = "none")]
+    pub fn as_str(&self) -> &str {
+        self.iter.as_str()
+    }
+}
+
+#[unstable(feature = "string_drain_as_str", issue = "none")]
+impl<'a> AsRef<str> for Drain<'a> {
+    fn as_ref(&self) -> &str {
+        self.as_str()
+    }
+}
+
 #[stable(feature = "drain", since = "1.6.0")]
 impl Iterator for Drain<'_> {
     type Item = char;