Skip to content

Commit 4838953

Browse files
authored
Rollup merge of rust-lang#63000 - max-sixty:chars-display, r=alexcrichton
Impl Debug for Chars Closes rust-lang#62947, making `Debug` more consistent with the struct's output and purpose Let me know any feedback!
2 parents 022f7a7 + 3325ff6 commit 4838953

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/liballoc/tests/str.rs

+10
Original file line numberDiff line numberDiff line change
@@ -1108,6 +1108,16 @@ fn test_iterator_last() {
11081108
assert_eq!(it.last(), Some('m'));
11091109
}
11101110

1111+
#[test]
1112+
fn test_chars_debug() {
1113+
let s = "ศไทย中华Việt Nam";
1114+
let c = s.chars();
1115+
assert_eq!(
1116+
format!("{:?}", c),
1117+
r#"Chars(['ศ', 'ไ', 'ท', 'ย', '中', '华', 'V', 'i', 'ệ', 't', ' ', 'N', 'a', 'm'])"#
1118+
);
1119+
}
1120+
11111121
#[test]
11121122
fn test_bytesator() {
11131123
let s = "ศไทย中华Việt Nam";

src/libcore/str/mod.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ Section: Iterators
464464
///
465465
/// [`chars`]: ../../std/primitive.str.html#method.chars
466466
/// [`str`]: ../../std/primitive.str.html
467-
#[derive(Clone, Debug)]
467+
#[derive(Clone)]
468468
#[stable(feature = "rust1", since = "1.0.0")]
469469
pub struct Chars<'a> {
470470
iter: slice::Iter<'a, u8>
@@ -600,6 +600,16 @@ impl<'a> Iterator for Chars<'a> {
600600
}
601601
}
602602

603+
#[stable(feature = "chars_debug_impl", since = "1.38.0")]
604+
impl fmt::Debug for Chars<'_> {
605+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
606+
write!(f, "Chars(")?;
607+
f.debug_list().entries(self.clone()).finish()?;
608+
write!(f, ")")?;
609+
Ok(())
610+
}
611+
}
612+
603613
#[stable(feature = "rust1", since = "1.0.0")]
604614
impl<'a> DoubleEndedIterator for Chars<'a> {
605615
#[inline]

0 commit comments

Comments
 (0)