Skip to content

Commit

Permalink
Rollup merge of #101529 - mousetail:patch-2, r=thomcc
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
GuillaumeGomez authored Sep 9, 2022
2 parents e20cc7d + b0925d8 commit 0534e9c
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion core/src/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1819,14 +1819,15 @@ impl<'a> Formatter<'a> {
/// write!(formatter,
/// "Foo({}{})",
/// if self.0 < 0 { '-' } else { '+' },
/// self.0)
/// self.0.abs())
/// } else {
/// write!(formatter, "Foo({})", self.0)
/// }
/// }
/// }
///
/// assert_eq!(&format!("{:+}", Foo(23)), "Foo(+23)");
/// assert_eq!(&format!("{:+}", Foo(-23)), "Foo(-23)");
/// assert_eq!(&format!("{}", Foo(23)), "Foo(23)");
/// ```
#[must_use]
Expand Down

0 comments on commit 0534e9c

Please sign in to comment.