Skip to content

Commit 076514c

Browse files
committed
add str::SplitInclusive::as_str method
This commit entroduces `core::str::SplitInclusive::as_str` method similar to `core::str::Split::as_str`, but under different gate - "str_split_inclusive_as_str" (this is done so because `SplitInclusive` is itself unstable).
1 parent 4747215 commit 076514c

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

library/core/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@
126126
#![feature(std_internals)]
127127
#![feature(stmt_expr_attributes)]
128128
#![feature(str_split_as_str)]
129+
#![feature(str_split_inclusive_as_str)]
129130
#![feature(transparent_unions)]
130131
#![feature(unboxed_closures)]
131132
#![feature(unsized_locals)]

library/core/src/str/iter.rs

+23-3
Original file line numberDiff line numberDiff line change
@@ -699,9 +699,7 @@ impl<'a, P: Pattern<'a>> SplitInternal<'a, P> {
699699
}
700700

701701
// SAFETY: `self.start` and `self.end` always lie on unicode boundaries.
702-
unsafe {
703-
self.matcher.haystack().get_unchecked(self.start..self.end)
704-
}
702+
unsafe { self.matcher.haystack().get_unchecked(self.start..self.end) }
705703
}
706704
}
707705

@@ -1278,6 +1276,28 @@ impl<'a, P: Pattern<'a, Searcher: ReverseSearcher<'a>>> DoubleEndedIterator
12781276
#[unstable(feature = "split_inclusive", issue = "72360")]
12791277
impl<'a, P: Pattern<'a>> FusedIterator for SplitInclusive<'a, P> {}
12801278

1279+
impl<'a, P: Pattern<'a>> SplitInclusive<'a, P> {
1280+
/// Returns remainder of the splitted string
1281+
///
1282+
/// # Examples
1283+
///
1284+
/// ```
1285+
/// #![feature(str_split_inclusive_as_str)]
1286+
/// #![feature(split_inclusive)]
1287+
/// let mut split = "Mary had a little lamb".split_inclusive(' ');
1288+
/// assert_eq!(split.as_str(), "Mary had a little lamb");
1289+
/// split.next();
1290+
/// assert_eq!(split.as_str(), "had a little lamb");
1291+
/// split.by_ref().for_each(drop);
1292+
/// assert_eq!(split.as_str(), "");
1293+
/// ```
1294+
#[inline]
1295+
#[unstable(feature = "str_split_inclusive_as_str", issue = "none")]
1296+
pub fn as_str(&self) -> &'a str {
1297+
self.0.as_str()
1298+
}
1299+
}
1300+
12811301
/// An iterator of [`u16`] over the string encoded as UTF-16.
12821302
///
12831303
/// This struct is created by the [`encode_utf16`] method on [`str`].

0 commit comments

Comments
 (0)