diff --git a/src/libcollections/btree/map.rs b/src/libcollections/btree/map.rs index 93978e31de5..e1d007f0ac4 100644 --- a/src/libcollections/btree/map.rs +++ b/src/libcollections/btree/map.rs @@ -904,11 +904,7 @@ fn cmp(&self, other: &BTreeMap) -> Ordering { #[stable(feature = "rust1", since = "1.0.0")] impl Debug for BTreeMap { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - let mut builder = f.debug_map(); - for (k, v) in self { - builder = builder.entry(k, v); - } - builder.finish() + self.iter().fold(f.debug_map(), |b, (k, v)| b.entry(k, v)).finish() } } diff --git a/src/libcollections/btree/set.rs b/src/libcollections/btree/set.rs index db0cdd30b1b..840110b5b27 100644 --- a/src/libcollections/btree/set.rs +++ b/src/libcollections/btree/set.rs @@ -628,11 +628,7 @@ fn bitor(self, rhs: &BTreeSet) -> BTreeSet { #[stable(feature = "rust1", since = "1.0.0")] impl Debug for BTreeSet { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - let mut builder = f.debug_set(); - for x in self { - builder = builder.entry(x); - } - builder.finish() + self.iter().fold(f.debug_set(), |b, e| b.entry(e)).finish() } } diff --git a/src/libcollections/linked_list.rs b/src/libcollections/linked_list.rs index 56c880ca6e2..581bab5759f 100644 --- a/src/libcollections/linked_list.rs +++ b/src/libcollections/linked_list.rs @@ -927,11 +927,7 @@ fn clone(&self) -> LinkedList { #[stable(feature = "rust1", since = "1.0.0")] impl fmt::Debug for LinkedList { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - let mut builder = f.debug_list(); - for e in self { - builder = builder.entry(e); - } - builder.finish() + self.iter().fold(f.debug_list(), |b, e| b.entry(e)).finish() } } diff --git a/src/libcore/fmt/mod.rs b/src/libcore/fmt/mod.rs index 856e7569f1c..29750a0a496 100644 --- a/src/libcore/fmt/mod.rs +++ b/src/libcore/fmt/mod.rs @@ -1017,22 +1017,7 @@ fn fmt(&self, f: &mut Formatter) -> Result { f.pad("&Any") } #[stable(feature = "rust1", since = "1.0.0")] impl Debug for [T] { fn fmt(&self, f: &mut Formatter) -> Result { - if f.flags & (1 << (FlagV1::Alternate as u32)) == 0 { - try!(write!(f, "[")); - } - let mut is_first = true; - for x in self { - if is_first { - is_first = false; - } else { - try!(write!(f, ", ")); - } - try!(write!(f, "{:?}", *x)) - } - if f.flags & (1 << (FlagV1::Alternate as u32)) == 0 { - try!(write!(f, "]")); - } - Ok(()) + self.iter().fold(f.debug_list(), |b, e| b.entry(e)).finish() } } diff --git a/src/libstd/collections/hash/map.rs b/src/libstd/collections/hash/map.rs index fd62729cd8f..50f405ea3b8 100644 --- a/src/libstd/collections/hash/map.rs +++ b/src/libstd/collections/hash/map.rs @@ -1226,11 +1226,7 @@ impl Debug for HashMap where K: Eq + Hash + Debug, V: Debug, S: HashState { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - let mut builder = f.debug_map(); - for (k, v) in self.iter() { - builder = builder.entry(k, v); - } - builder.finish() + self.iter().fold(f.debug_map(), |b, (k, v)| b.entry(k, v)).finish() } } diff --git a/src/libstd/collections/hash/set.rs b/src/libstd/collections/hash/set.rs index 44c3d8262a7..d3dae88265a 100644 --- a/src/libstd/collections/hash/set.rs +++ b/src/libstd/collections/hash/set.rs @@ -614,11 +614,7 @@ impl fmt::Debug for HashSet S: HashState { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - let mut builder = f.debug_set(); - for x in self { - builder = builder.entry(x); - } - builder.finish() + self.iter().fold(f.debug_set(), |b, e| b.entry(e)).finish() } }