about summary refs log tree commit diff
diff options
context:
space:
mode:
authorYuki Okushi <huyuumi.dev@gmail.com>2021-03-27 12:37:20 +0900
committerGitHub <noreply@github.com>2021-03-27 12:37:20 +0900
commitc1432679017cadb3b0dd3c6c7653e8ba138e4c63 (patch)
tree82e94b9c772b3191fc4496bed37d250549ee8adf
parentd7216bae233d8e5f1191ec0c7dcf741789a235b0 (diff)
parent93737dc634f42d07441db2acf26a88ae2e888d9f (diff)
downloadrust-c1432679017cadb3b0dd3c6c7653e8ba138e4c63.tar.gz
rust-c1432679017cadb3b0dd3c6c7653e8ba138e4c63.zip
Rollup merge of #83388 - alamb:alamb/fmt-dcs, r=Mark-Simulacrum
Make # pretty print format easier to discover

# Rationale:

I use (cargo cult?) three formats in rust:  `{}`, debug `{:?}`, and pretty-print debug `{:#?}`. I discovered `{:#?}` in some blog post or guide when I started working in Rust. While `#` is documented I think it is hard to discover. So taking the good advice of ```@carols10cents```  I am trying to improve the docs with a PR

As a reminder "pretty print" means that where `{:?}` will print something like
```
foo: { b1: 1, b2: 2}
```

`{:#?}` will prints something like
```
foo {
  b1: 1
  b2: 3
}
```

# Changes
Add an example to `fmt` to try and make it easier to discover `#`
-rw-r--r--library/alloc/src/fmt.rs6
1 files changed, 5 insertions, 1 deletions
diff --git a/library/alloc/src/fmt.rs b/library/alloc/src/fmt.rs
index f9424b1d747..e765fa9d14c 100644
--- a/library/alloc/src/fmt.rs
+++ b/library/alloc/src/fmt.rs
@@ -19,6 +19,10 @@
 //! format!("{value}", value=4);      // => "4"
 //! format!("{} {}", 1, 2);           // => "1 2"
 //! format!("{:04}", 42);             // => "0042" with leading zeros
+//! format!("{:#?}", (100, 200));     // => "(
+//!                                   //       100,
+//!                                   //       200,
+//!                                   //     )"
 //! ```
 //!
 //! From these, you can see that the first argument is a format string. It is
@@ -163,7 +167,7 @@
 //! * `-` - Currently not used
 //! * `#` - This flag indicates that the "alternate" form of printing should
 //!         be used. The alternate forms are:
-//!     * `#?` - pretty-print the [`Debug`] formatting
+//!     * `#?` - pretty-print the [`Debug`] formatting (adds linebreaks and indentation)
 //!     * `#x` - precedes the argument with a `0x`
 //!     * `#X` - precedes the argument with a `0x`
 //!     * `#b` - precedes the argument with a `0b`