Skip to content

Commit 46bb884

Browse files
authored
Rollup merge of #76525 - fusion-engineering-forks:string-drain, r=dtolnay
Add as_str() to string::Drain. Vec's Drain recently [had its `.as_slice()` stabilized](#72584), but String's Drain was still missing the analogous `.as_str()`. This adds that. Also improves the Debug implementation, which now shows the remaining data instead of just `"Drain { .. }"`.
2 parents fef3324 + 15eb638 commit 46bb884

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

library/alloc/src/string.rs

+35-1
Original file line numberDiff line numberDiff line change
@@ -2440,7 +2440,7 @@ pub struct Drain<'a> {
24402440
#[stable(feature = "collection_debug", since = "1.17.0")]
24412441
impl fmt::Debug for Drain<'_> {
24422442
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2443-
f.pad("Drain { .. }")
2443+
f.debug_tuple("Drain").field(&self.as_str()).finish()
24442444
}
24452445
}
24462446

@@ -2463,6 +2463,40 @@ impl Drop for Drain<'_> {
24632463
}
24642464
}
24652465

2466+
impl<'a> Drain<'a> {
2467+
/// Returns the remaining (sub)string of this iterator as a slice.
2468+
///
2469+
/// # Examples
2470+
///
2471+
/// ```
2472+
/// #![feature(string_drain_as_str)]
2473+
/// let mut s = String::from("abc");
2474+
/// let mut drain = s.drain(..);
2475+
/// assert_eq!(drain.as_str(), "abc");
2476+
/// let _ = drain.next().unwrap();
2477+
/// assert_eq!(drain.as_str(), "bc");
2478+
/// ```
2479+
#[unstable(feature = "string_drain_as_str", issue = "76905")] // Note: uncomment AsRef impls below when stabilizing.
2480+
pub fn as_str(&self) -> &str {
2481+
self.iter.as_str()
2482+
}
2483+
}
2484+
2485+
// Uncomment when stabilizing `string_drain_as_str`.
2486+
// #[unstable(feature = "string_drain_as_str", issue = "76905")]
2487+
// impl<'a> AsRef<str> for Drain<'a> {
2488+
// fn as_ref(&self) -> &str {
2489+
// self.as_str()
2490+
// }
2491+
// }
2492+
//
2493+
// #[unstable(feature = "string_drain_as_str", issue = "76905")]
2494+
// impl<'a> AsRef<[u8]> for Drain<'a> {
2495+
// fn as_ref(&self) -> &[u8] {
2496+
// self.as_str().as_bytes()
2497+
// }
2498+
// }
2499+
24662500
#[stable(feature = "drain", since = "1.6.0")]
24672501
impl Iterator for Drain<'_> {
24682502
type Item = char;

0 commit comments

Comments
 (0)