diff options
| author | bors <bors@rust-lang.org> | 2016-07-21 16:19:54 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2016-07-21 16:19:54 -0700 |
| commit | 62690b3c3fa6e157daa7f7c5a3af005f37eb4199 (patch) | |
| tree | 3781e11e755200990ea0a30f08df95f5f7c88d3b /src/libcollections | |
| parent | 75886537858530447051b9c1ef595c4ba59017c5 (diff) | |
| parent | ede39aeb331bf6efb3739d22a60c1844e9c2c3d6 (diff) | |
| download | rust-62690b3c3fa6e157daa7f7c5a3af005f37eb4199.tar.gz rust-62690b3c3fa6e157daa7f7c5a3af005f37eb4199.zip | |
Auto merge of #34544 - 3Hren:issue/xx/reinterpret-format-precision-for-strings, r=alexcrichton
feat: reinterpret `precision` field for strings
This commit changes the behavior of formatting string arguments with both width and precision fields set.
Documentation says that the `width` field is the "minimum width" that the format should take up. If the value's string does not fill up this many characters, then the padding specified by fill/alignment will be used to take up the required space.
This is true for all formatted types except string, which is truncated down to `precision` number of chars and then all of `fill`, `align` and `width` fields are completely ignored.
For example: `format!("{:/^10.8}", "1234567890);` emits "12345678". In the contrast Python version works as the expected:
```python
>>> '{:/^10.8}'.format('1234567890')
'/12345678/'
```
This commit gives back the `Python` behavior by changing the `precision` field meaning to the truncation and nothing more. The result string *will* be prepended/appended up to the `width` field with the proper `fill` char.
__However, this is the breaking change, I admit.__ Feel free to close it, but otherwise it should be mentioned in the `std::fmt` documentation somewhere near of `fill/align/width` fields description.
Diffstat (limited to 'src/libcollections')
| -rw-r--r-- | src/libcollections/fmt.rs | 6 |
1 files changed, 4 insertions, 2 deletions
diff --git a/src/libcollections/fmt.rs b/src/libcollections/fmt.rs index 15de0dd802d..c5312d0d9bb 100644 --- a/src/libcollections/fmt.rs +++ b/src/libcollections/fmt.rs @@ -409,8 +409,8 @@ //! ## Precision //! //! For non-numeric types, this can be considered a "maximum width". If the resulting string is -//! longer than this width, then it is truncated down to this many characters and only those are -//! emitted. +//! longer than this width, then it is truncated down to this many characters and that truncated +//! value is emitted with proper `fill`, `alignment` and `width` if those parameters are set. //! //! For integral types, this is ignored. //! @@ -470,6 +470,7 @@ //! ``` //! println!("{}, `{name:.*}` has 3 fractional digits", "Hello", 3, name=1234.56); //! println!("{}, `{name:.*}` has 3 characters", "Hello", 3, name="1234.56"); +//! println!("{}, `{name:>8.*}` has 3 right-aligned characters", "Hello", 3, name="1234.56"); //! ``` //! //! print two significantly different things: @@ -477,6 +478,7 @@ //! ```text //! Hello, `1234.560` has 3 fractional digits //! Hello, `123` has 3 characters +//! Hello, ` 123` has 3 right-aligned characters //! ``` //! //! # Escaping |
