about summary refs log tree commit diff
path: root/src/libcore/str
diff options
context:
space:
mode:
authorSimon Sapin <simon.sapin@exyr.org>2015-08-19 17:31:46 +0200
committerSimon Sapin <simon.sapin@exyr.org>2015-08-28 22:44:17 +0200
commite33650c16fec1ed8dcdbfc0aa3eac33ceece34d3 (patch)
tree53e4494c4d585813c7360d78d5c34356343608cb /src/libcore/str
parent6f28232756a6842acb1d2da6defdea43cfe95dde (diff)
downloadrust-e33650c16fec1ed8dcdbfc0aa3eac33ceece34d3.tar.gz
rust-e33650c16fec1ed8dcdbfc0aa3eac33ceece34d3.zip
Add .as_str() to str::Chars and str::CharIndices. See #27775.
Diffstat (limited to 'src/libcore/str')
-rw-r--r--src/libcore/str/mod.rs24
1 files changed, 24 insertions, 0 deletions
diff --git a/src/libcore/str/mod.rs b/src/libcore/str/mod.rs
index 48118c18029..7aeda24a290 100644
--- a/src/libcore/str/mod.rs
+++ b/src/libcore/str/mod.rs
@@ -292,6 +292,18 @@ impl<'a> DoubleEndedIterator for Chars<'a> {
     }
 }
 
+impl<'a> Chars<'a> {
+    /// View the underlying data as a subslice of the original data.
+    ///
+    /// This has the same lifetime as the original slice, and so the
+    /// iterator can continue to be used while this exists.
+    #[unstable(feature = "iter_to_slice", issue = "27775")]
+    #[inline]
+    pub fn as_str(&self) -> &'a str {
+        unsafe { from_utf8_unchecked(self.iter.as_slice()) }
+    }
+}
+
 /// Iterator for a string's characters and their byte offsets.
 #[derive(Clone)]
 #[stable(feature = "rust1", since = "1.0.0")]
@@ -339,6 +351,18 @@ impl<'a> DoubleEndedIterator for CharIndices<'a> {
     }
 }
 
+impl<'a> CharIndices<'a> {
+    /// View the underlying data as a subslice of the original data.
+    ///
+    /// This has the same lifetime as the original slice, and so the
+    /// iterator can continue to be used while this exists.
+    #[unstable(feature = "iter_to_slice", issue = "27775")]
+    #[inline]
+    pub fn as_str(&self) -> &'a str {
+        self.iter.as_str()
+    }
+}
+
 /// External iterator for a string's bytes.
 /// Use with the `std::iter` module.
 ///