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](https://github.com/rust-lang/rust/pull/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 { .. }"`.
This commit is contained in:
commit
46bb884cf3
|
@ -2440,7 +2440,7 @@ pub struct Drain<'a> {
|
|||
#[stable(feature = "collection_debug", since = "1.17.0")]
|
||||
impl fmt::Debug for Drain<'_> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.pad("Drain { .. }")
|
||||
f.debug_tuple("Drain").field(&self.as_str()).finish()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -2463,6 +2463,40 @@ impl Drop for Drain<'_> {
|
|||
}
|
||||
}
|
||||
|
||||
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 = "76905")] // Note: uncomment AsRef impls below when stabilizing.
|
||||
pub fn as_str(&self) -> &str {
|
||||
self.iter.as_str()
|
||||
}
|
||||
}
|
||||
|
||||
// Uncomment when stabilizing `string_drain_as_str`.
|
||||
// #[unstable(feature = "string_drain_as_str", issue = "76905")]
|
||||
// impl<'a> AsRef<str> for Drain<'a> {
|
||||
// fn as_ref(&self) -> &str {
|
||||
// self.as_str()
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// #[unstable(feature = "string_drain_as_str", issue = "76905")]
|
||||
// impl<'a> AsRef<[u8]> for Drain<'a> {
|
||||
// fn as_ref(&self) -> &[u8] {
|
||||
// self.as_str().as_bytes()
|
||||
// }
|
||||
// }
|
||||
|
||||
#[stable(feature = "drain", since = "1.6.0")]
|
||||
impl Iterator for Drain<'_> {
|
||||
type Item = char;
|
||||
|
|
Loading…
Reference in New Issue