Skip to content

Commit fcdd501

Browse files
authored
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.
2 parents bef48f9 + 5fbe485 commit fcdd501

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

library/core/src/fmt/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1819,14 +1819,15 @@ impl<'a> Formatter<'a> {
18191819
/// write!(formatter,
18201820
/// "Foo({}{})",
18211821
/// if self.0 < 0 { '-' } else { '+' },
1822-
/// self.0)
1822+
/// self.0.abs())
18231823
/// } else {
18241824
/// write!(formatter, "Foo({})", self.0)
18251825
/// }
18261826
/// }
18271827
/// }
18281828
///
18291829
/// assert_eq!(&format!("{:+}", Foo(23)), "Foo(+23)");
1830+
/// assert_eq!(&format!("{:+}", Foo(-23)), "Foo(-23)");
18301831
/// assert_eq!(&format!("{}", Foo(23)), "Foo(23)");
18311832
/// ```
18321833
#[must_use]

0 commit comments

Comments
 (0)