about summary refs log tree commit diff
path: root/library/core/src/fmt/mod.rs
AgeCommit message (Collapse)AuthorLines
2025-09-26formatting_options: fix alternate docs 0b/0o mixupDaniel Verkamp-2/+2
The descriptions of the alternate forms of Octal and Binary were swapped in the doc comment for FormattingOptions::alternate().
2025-08-26formatting_options: Make all methods `const`Elias Holzmann-11/+11
Having `const fn`s that take a `mut &` was unstable until Rust 1.83. Because of this, not all methods on `FormattingOptions` were implemented as `const`. As this has been stabilized now, there is no reason not to have all methods `const`. Thanks to Ternvein for bringing this to my attention (see [1]). [1]: https://github.com/rust-lang/rust/issues/118117#issuecomment-2687470635
2025-07-20Rollup merge of #143423 - hkBst:clippy-fix-1, r=workingjubileeGuillaume Gomez-1/+0
address clippy formatting nits - int_log10.rs: change top level doc comments to outer - collect.rs: remove empty line after doc comment - clippy fix: markdown indentation for indented items after line break: a markdown list item continued over multiples lines, but those following lines which are part of the same item are not indented - clippy fix: bound in one place: when there is a bound in angle brackets and another bound on the same variable in a where clause
2025-07-08Rollup merge of #143426 - hkBst:clippy-fix-indent-1, r=jhprattTrevor Gross-7/+7
clippy fix: indentation Fixes indentation of markdown comments.
2025-07-08collect.rs: remove empty line after doc commentMarijn Schouten-1/+0
2025-07-04clippy fix: indentationMarijn Schouten-7/+7
2025-07-03Implement `int_format_into` featureGuillaume Gomez-0/+4
2025-07-02Remove some unsized tuple impls now that we don't support unsizing tuples ↵Oli Scherer-6/+1
anymore
2025-06-22Implement DesugaringKind::FormatLiteralmejrs-7/+12
2025-06-16library/compiler: add `PointeeSized` boundsDavid Wood-18/+18
As core uses an extern type (`ptr::VTable`), the default `?Sized` to `MetaSized` migration isn't sufficient, and some code that previously accepted `VTable` needs relaxed to continue to accept extern types. Similarly, the compiler uses many extern types in `rustc_codegen_llvm` and in the `rustc_middle::ty::List` implementation (`OpaqueListContents`) some bounds must be relaxed to continue to accept these types. Unfortunately, due to the current inability to relax `Deref::Target`, some of the bounds in the standard library are forced to be stricter than they ideally would be.
2025-06-03`Display`: Rework explanation of `FromStr`/`Display` round-trippingJosh Triplett-2/+7
- Drop "usually a mistake" - Add phrasing from `FromStr` about round-tripping, and about how the inability to round-trip may surprise users.
2025-06-03Improve the documentation of `Display` and `FromStr`, and their interactionsJosh Triplett-0/+9
In particular: - `Display` is not necessarily lossless - The output of `Display` might not be parseable by `FromStr`, and might not produce the same value if it is. - Calling `.parse()` on the output of `Display` is usually a mistake unless a type's documented output and input formats match. - The input formats accepted by `FromStr` depend on the type.
2025-05-17Switch library rustc_unimplemented to use `Self` and `This`mejrs-5/+5
2025-05-01Move core::fmt::Arguments::new_v1* to rt.rs.Mara Bos-35/+0
2025-04-15Use full path for core::mem::transmuteAlice Ryhl-1/+1
Suggested-by: Tamir Duberstein <tamird@gmail.com> Signed-off-by: Alice Ryhl <aliceryhl@google.com>
2025-04-12cfg(kcfi)Alice Ryhl-1/+1
2025-04-10cfi: do not transmute function pointers in formatting codeAlice Ryhl-1/+1
2025-04-09update cfgsBoxy-28/+0
2025-04-06Rollup merge of #139123 - thaliaarchi:core-alloc-test-paths, r=bjorn3Stuart Cook-2/+2
tidy: Fix paths to `coretests` and `alloctests` Following `#135937` and `#136642`, tests for core and alloc are in coretests and alloctests. Fix tidy to lint for the new paths. Also, update comments referring to the old locations. Some context for changes which don't match that pattern: - `library/std/src/thread/local/dynamic_tests.rs` and `library/std/src/sync/mpsc/sync_tests.rs` were moved under `library/std/tests/` in 332fb7e6f1d (Move std::thread_local unit tests to integration tests, 2025-01-17) and b8ae372e483 (Move std::sync unit tests to integration tests, 2025-01-17), respectively, so are no longer special cases. - There never was a `library/core/tests/fmt.rs` file. That comment previously referred to `src/test/ui/ifmt.rs`, which was folded into `library/alloc/tests/fmt.rs` in 949c96660c3 (move format! interface tests, 2020-09-08). Now, the only matches for `(alloc|core)/tests` are in `compiler/rustc_codegen_{cranelift,gcc}/patches`. I don't know why CI hasn't broken because those patches can't apply. Or maybe they somehow still can apply? r? `@bjorn3`
2025-04-05tidy: Fix paths to coretests and alloctestsThalia Archibald-2/+2
Following `#135937` and `#136642`, tests for core and alloc are in coretests and alloctests. Fix tidy to lint for the new paths. Also, update comments referring to the old locations. Some context for changes which don't match that pattern: * library/std/src/thread/local/dynamic_tests.rs and library/std/src/sync/mpsc/sync_tests.rs were moved under library/std/tests/ in 332fb7e6f1d (Move std::thread_local unit tests to integration tests, 2025-01-17) and b8ae372e483 (Move std::sync unit tests to integration tests, 2025-01-17), respectively, so are no longer special cases. * There never was a library/core/tests/fmt.rs file. That comment previously referred to src/test/ui/ifmt.rs, which was folded into library/alloc/tests/fmt.rs in 949c96660c3 (move format! interface tests, 2020-09-08).
2025-04-04make `Arguments::as_statically_known_str` doc(hidden)mejrs-0/+1
2025-03-22Auto merge of #136974 - m-ou-se:fmt-options-64-bit, r=scottmcmbors-173/+221
Reduce FormattingOptions to 64 bits This is part of https://github.com/rust-lang/rust/issues/99012 This reduces FormattingOptions from 6-7 machine words (384 bits on 64-bit platforms, 224 bits on 32-bit platforms) to just 64 bits (a single register on 64-bit platforms). Before: ```rust pub struct FormattingOptions { flags: u32, // only 6 bits used fill: char, align: Option<Alignment>, width: Option<usize>, precision: Option<usize>, } ``` After: ```rust pub struct FormattingOptions { /// Bits: /// - 0-20: fill character (21 bits, a full `char`) /// - 21: `+` flag /// - 22: `-` flag /// - 23: `#` flag /// - 24: `0` flag /// - 25: `x?` flag /// - 26: `X?` flag /// - 27: Width flag (if set, the width field below is used) /// - 28: Precision flag (if set, the precision field below is used) /// - 29-30: Alignment (0: Left, 1: Right, 2: Center, 3: Unknown) /// - 31: Always set to 1 flags: u32, /// Width if width flag above is set. Otherwise, always 0. width: u16, /// Precision if precision flag above is set. Otherwise, always 0. precision: u16, } ```
2025-03-21Add todo comment on using a niche type for fmt flags.Mara Bos-0/+2
2025-03-18Optimize io::Write::write_fmt for constant stringsThalia Archibald-1/+2
When the formatting args to `fmt::Write::write_fmt` are a statically known string, it simplifies to only calling `write_str` without a runtime branch. Do the same in `io::Write::write_fmt` with `write_all`. Also, match the convention of `fmt::Write` for the name of `args`.
2025-03-16Rollup merge of #135080 - Enselic:debug-ptr-metadata, r=thomcc许杰友 Jieyou Xu (Joe)-1/+8
core: Make `Debug` impl of raw pointers print metadata if present Make Rust pointers appear less magic by including metadata information in their `Debug` output. This does not break Rust stability guarantees because `Debug` impl are explicitly exempted from stability: https://doc.rust-lang.org/std/fmt/trait.Debug.html#stability > ## Stability > > Derived `Debug` formats are not stable, and so may change with future Rust versions. Additionally, `Debug` implementations of types provided by the standard library (`std`, `core`, `alloc`, etc.) are not stable, and may also change with future Rust versions. Note that a regression test is added as a separate commit to make it clear what impact the last commit has on the output. Closes #128684 because the output of that code now becomes: ``` thread 'main' panicked at src/main.rs:5:5: assertion `left == right` failed left: Pointer { addr: 0x7ffd45c6fc6b, metadata: 5 } right: Pointer { addr: 0x7ffd45c6fc6b, metadata: 3 } note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace ```
2025-03-16Rollup merge of #138082 - thaliaarchi:slice-cfg-not-test, r=thomcc许杰友 Jieyou Xu (Joe)-1/+1
Remove `#[cfg(not(test))]` gates in `core` These gates are unnecessary now that unit tests for `core` are in a separate package, `coretests`, instead of in the same files as the source code. They previously prevented the two `core` versions from conflicting with each other.
2025-03-12Reduce FormattingOptions to 64 bits.Mara Bos-173/+219
2025-03-10Limit formatting width and precision to 16 bits.Mara Bos-24/+27
2025-03-06Remove #[cfg(not(test))] gates in coreThalia Archibald-1/+1
These gates are unnecessary now that unit tests for `core` are in a separate package, `coretests`, instead of in the same files as the source code. They previously prevented the two `core` versions from conflicting with each other.
2025-02-27Count char width at most once in Formatter::padThalia Archibald-37/+29
When both width and precision flags are specified, then the character width is counted twice. Instead, record the character width when truncating it to the precision, so it does not need to be recomputed. Simplify control flow so the cases are more clear.
2025-02-16add MAX_LEN_UTF8 and MAX_LEN_UTF16 constantsHTGAzureX1212-3/+3
2025-02-15core: Make `Debug` impl of raw pointers print metadata if presentMartin Nordholts-1/+8
Make Rust pointers less magic by including metadata information in their `Debug` output. This does not break Rust stability guarantees because `Debug` output is explicitly exempted from stability: https://doc.rust-lang.org/std/fmt/trait.Debug.html#stability Co-authored-by: Lukas <26522220+lukas-code@users.noreply.github.com> Co-authored-by: Josh Stone <cuviper@gmail.com>
2025-01-24Fix `FormattingOptions` instantiation with `Default`Yuri Astrakhan-1/+10
The `fill` value by default should be set to `' '` (space), but the current implementation uses `#[derive(Default)]` which sets it to `\0`
2025-01-05[generic_assert] Constify methods used by the formatting systemCaio-2/+2
2024-12-21Less unwrap() in documentationKornel-4/+7
2024-12-05Access members of `FormattingOptions` directly instead of via getters/settersElias Holzmann-38/+36
2024-12-05Removed constness for methods receiving a `&mut` parameterElias Holzmann-11/+11
See https://github.com/rust-lang/rust/pull/118159#discussion_r1495760867 for context.
2024-12-05Added better reason for exposing `flags` and `get_flags` as unstableElias Holzmann-2/+10
2024-12-05FormattedElias Holzmann-12/+10
2024-12-05Refactored FormattingOptions to use a bitmask for storing flagsElias Holzmann-44/+46
2024-12-05Revert "Turned public+unstable+hidden functions into private functions"Elias Holzmann-2/+6
See https://github.com/rust-lang/rust/pull/118159#discussion_r1491842170 for context. This reverts commit 62078dffcc1aefd4d678df94bca06e7b864065bd.
2024-12-05Turned public+unstable+hidden functions into private functionsElias Holzmann-6/+2
2024-12-05Made all fns constElias Holzmann-22/+22
2024-12-05impl Default for fmt::FormattingOptionsElias Holzmann-1/+1
2024-12-05Fixed copy+paste error in commentElias Holzmann-1/+1
Co-authored-by: Mara Bos <m-ou.se@m-ou.se>
2024-12-05fmt::FormattingOptions: Renamed `alignment` to `align`Elias Holzmann-13/+13
Likewise for `get_alignment`. This is how the method is named on `Formatter`, I want to keep it consistent.
2024-12-05Formatter::with_options: Use different lifetimesElias Holzmann-1/+1
Formatter::with_options takes self as a mutable reference (`&'a mut Formatter<'b>`). `'a` and `'b` need to be different lifetimes. Just taking `&'a mut Formatter<'a>` and trusting in Rust being able to implicitely convert from `&'a mut Formatter<'b>` if necessary (after all, `'a` must be smaller than `'b` anyway) fails because `'b` is behind a *mutable* reference. For background on on this behavior, see https://doc.rust-lang.org/nomicon/subtyping.html#variance.
2024-12-05Added struct `fmt::FormattingOptions`Elias Holzmann-68/+313
This allows to build custom `std::Formatter`s at runtime. Also added some related enums and two related methods on `std::Formatter`.
2024-12-05Formatter: Access members via getter methods wherever possibleElias Holzmann-13/+13
The idea behind this is to make implementing `fmt::FormattingOptions` (as well as any future changes to `std::Formatter`) easier. In theory, this might have a negative performance impact because of the additional function calls. However, I strongly believe that those will be inlined anyway, thereby producing assembly code that has comparable performance.
2024-11-27update cfgsBoxy-4/+0