about summary refs log tree commit diff
diff options
context:
space:
mode:
authorGuillaume Gomez <guillaume1.gomez@gmail.com>2022-09-09 15:36:37 +0200
committerGitHub <noreply@github.com>2022-09-09 15:36:37 +0200
commitff21ccfba18760659aa81c9b7e347d54a0ba2833 (patch)
treee22d38f4e43429ec4c218c57363e0fd1aac324d1
parent3ec332fc82596e7f80311fa142035945729ce156 (diff)
parent5fbe485ecca977e3981d9dbcc5370183528d50a7 (diff)
downloadrust-ff21ccfba18760659aa81c9b7e347d54a0ba2833.tar.gz
rust-ff21ccfba18760659aa81c9b7e347d54a0ba2833.zip
Rollup merge of #101529 - mousetail:patch-2, r=thomcc
Fix the example code and doctest for Formatter::sign_plus

The provided example to the `sign_plus` method on `fmt` was broken, it displays the `-` sign twice for negative numbers.

This pull request should fix the issue by `.abs()` ing the number so that the negative sign appears only once. It is just one possible solution to the issue, not sure if it's the best. However, this one will behave as expected when combined with fill and alignment operators.
-rw-r--r--library/core/src/fmt/mod.rs3
1 files changed, 2 insertions, 1 deletions
diff --git a/library/core/src/fmt/mod.rs b/library/core/src/fmt/mod.rs
index 9d3e9abf557..905212eb372 100644
--- a/library/core/src/fmt/mod.rs
+++ b/library/core/src/fmt/mod.rs
@@ -1819,7 +1819,7 @@ impl<'a> Formatter<'a> {
     ///             write!(formatter,
     ///                    "Foo({}{})",
     ///                    if self.0 < 0 { '-' } else { '+' },
-    ///                    self.0)
+    ///                    self.0.abs())
     ///         } else {
     ///             write!(formatter, "Foo({})", self.0)
     ///         }
@@ -1827,6 +1827,7 @@ impl<'a> Formatter<'a> {
     /// }
     ///
     /// assert_eq!(&format!("{:+}", Foo(23)), "Foo(+23)");
+    /// assert_eq!(&format!("{:+}", Foo(-23)), "Foo(-23)");
     /// assert_eq!(&format!("{}", Foo(23)), "Foo(23)");
     /// ```
     #[must_use]