diff options
| author | Sean McArthur <sean.monstar@gmail.com> | 2014-12-20 00:09:35 -0800 |
|---|---|---|
| committer | Sean McArthur <sean.monstar@gmail.com> | 2015-01-06 14:49:42 -0800 |
| commit | 44440e5c18a1dbcc9685866ffffe00c508929079 (patch) | |
| tree | b66c50cd1e471dc0e37b8a0db2cf7f8f28fbac5f /src/libstd/collections | |
| parent | 8efd9901b628d687d11a4d0ccc153553b38ada49 (diff) | |
core: split into fmt::Show and fmt::String
fmt::Show is for debugging, and can and should be implemented for
all public types. This trait is used with `{:?}` syntax. There still
exists #[derive(Show)].
fmt::String is for types that faithfully be represented as a String.
Because of this, there is no way to derive fmt::String, all
implementations must be purposeful. It is used by the default format
syntax, `{}`.
This will break most instances of `{}`, since that now requires the type
to impl fmt::String. In most cases, replacing `{}` with `{:?}` is the
correct fix. Types that were being printed specifically for users should
receive a fmt::String implementation to fix this.
Part of #20013
[breaking-change]
Diffstat (limited to 'src/libstd/collections')
| -rw-r--r-- | src/libstd/collections/hash/map.rs | 12 | ||||
| -rw-r--r-- | src/libstd/collections/hash/set.rs | 12 |
2 files changed, 12 insertions, 12 deletions
diff --git a/src/libstd/collections/hash/map.rs b/src/libstd/collections/hash/map.rs index a3fc38c34e8..681f2454fa0 100644 --- a/src/libstd/collections/hash/map.rs +++ b/src/libstd/collections/hash/map.rs @@ -292,7 +292,7 @@ fn test_resize_policy() { /// /// // Use derived implementation to print the status of the vikings. /// for (viking, health) in vikings.iter() { -/// println!("{} has {} hp", viking, health); +/// println!("{:?} has {} hp", viking, health); /// } /// ``` #[derive(Clone)] @@ -1207,11 +1207,11 @@ impl<K: Eq + Hash<S>, V: Eq, S, H: Hasher<S>> Eq for HashMap<K, V, H> {} #[stable] impl<K: Eq + Hash<S> + Show, V: Show, S, H: Hasher<S>> Show for HashMap<K, V, H> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - try!(write!(f, "{{")); + try!(write!(f, "HashMap {{")); for (i, (k, v)) in self.iter().enumerate() { if i != 0 { try!(write!(f, ", ")); } - try!(write!(f, "{}: {}", *k, *v)); + try!(write!(f, "{:?}: {:?}", *k, *v)); } write!(f, "}}") @@ -1891,10 +1891,10 @@ mod test_map { map.insert(1i, 2i); map.insert(3i, 4i); - let map_str = format!("{}", map); + let map_str = format!("{:?}", map); - assert!(map_str == "{1: 2, 3: 4}" || map_str == "{3: 4, 1: 2}"); - assert_eq!(format!("{}", empty), "{}"); + assert!(map_str == "HashMap {1i: 2i, 3i: 4i}" || map_str == "{3i: 4i, 1i: 2i}"); + assert_eq!(format!("{:?}", empty), "HashMap {}"); } #[test] diff --git a/src/libstd/collections/hash/set.rs b/src/libstd/collections/hash/set.rs index 211bfe2c10e..10e8ca6c622 100644 --- a/src/libstd/collections/hash/set.rs +++ b/src/libstd/collections/hash/set.rs @@ -88,7 +88,7 @@ use super::map::{self, HashMap, Keys, INITIAL_CAPACITY}; /// /// // Use derived implementation to print the vikings. /// for x in vikings.iter() { -/// println!("{}", x); +/// println!("{:?}", x); /// } /// ``` #[derive(Clone)] @@ -585,11 +585,11 @@ impl<T: Eq + Hash<S>, S, H: Hasher<S>> Eq for HashSet<T, H> {} #[stable] impl<T: Eq + Hash<S> + fmt::Show, S, H: Hasher<S>> fmt::Show for HashSet<T, H> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - try!(write!(f, "{{")); + try!(write!(f, "HashSet {{")); for (i, x) in self.iter().enumerate() { if i != 0 { try!(write!(f, ", ")); } - try!(write!(f, "{}", *x)); + try!(write!(f, "{:?}", *x)); } write!(f, "}}") @@ -1116,10 +1116,10 @@ mod test_set { set.insert(1i); set.insert(2); - let set_str = format!("{}", set); + let set_str = format!("{:?}", set); - assert!(set_str == "{1, 2}" || set_str == "{2, 1}"); - assert_eq!(format!("{}", empty), "{}"); + assert!(set_str == "HashSet {1i, 2i}" || set_str == "{2i, 1i}"); + assert_eq!(format!("{:?}", empty), "HashSet {}"); } #[test] |
