Skip to content

Commit e33650c

Browse files
committed
Add .as_str() to str::Chars and str::CharIndices. See rust-lang#27775.
1 parent 6f28232 commit e33650c

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/libcore/str/mod.rs

+24
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,18 @@ impl<'a> DoubleEndedIterator for Chars<'a> {
292292
}
293293
}
294294

295+
impl<'a> Chars<'a> {
296+
/// View the underlying data as a subslice of the original data.
297+
///
298+
/// This has the same lifetime as the original slice, and so the
299+
/// iterator can continue to be used while this exists.
300+
#[unstable(feature = "iter_to_slice", issue = "27775")]
301+
#[inline]
302+
pub fn as_str(&self) -> &'a str {
303+
unsafe { from_utf8_unchecked(self.iter.as_slice()) }
304+
}
305+
}
306+
295307
/// Iterator for a string's characters and their byte offsets.
296308
#[derive(Clone)]
297309
#[stable(feature = "rust1", since = "1.0.0")]
@@ -339,6 +351,18 @@ impl<'a> DoubleEndedIterator for CharIndices<'a> {
339351
}
340352
}
341353

354+
impl<'a> CharIndices<'a> {
355+
/// View the underlying data as a subslice of the original data.
356+
///
357+
/// This has the same lifetime as the original slice, and so the
358+
/// iterator can continue to be used while this exists.
359+
#[unstable(feature = "iter_to_slice", issue = "27775")]
360+
#[inline]
361+
pub fn as_str(&self) -> &'a str {
362+
self.iter.as_str()
363+
}
364+
}
365+
342366
/// External iterator for a string's bytes.
343367
/// Use with the `std::iter` module.
344368
///

0 commit comments

Comments
 (0)