Skip to content

format-args/ #13

Open
Open

Description

@utterances-bot

Behind the Scenes of Rust String Formatting: format_args!() - Mara's Blog

The fmt::Arguments type is one of my favorite types in the Rust standard library. It’s not particularly amazing, but it is a great building block that is indirectly used in nearly every Rust program. This type, together with the format_args!() macro, is the power behind print!(), format!(), log::info!() and many more text formatting macros, both from the standard library and community crates.
In this blog post, we learn how it works, how it is implemented today, and how that might change in the future.

https://blog.m-ou.se/format-args/

Activity

  1. NobodyXu commented on Dec 6, 2023

    @NobodyXu

    For Pieces::String, I think it's a good idea to use a NonEmptyString instead of &'static str so that compiler can take advantages of that niche and reduce the size of enum, since the const string should never be empty.

  2. SpriteOvO commented on Dec 6, 2023

    @SpriteOvO

    This made me recall a pattern! macro that my co-contributor implemented for our logging library about a year ago. I've been planning to support more advanced flags (such as alignment and padding) in the future, by learning the implementation details from the standard library's format_args!, but don't know where to start. This post is definitely very helpful to me! 👍 Thanks!

  3. BartMassey commented on Dec 9, 2023

    @BartMassey

    This is really cool work. Thank you for doing it. I'm really curious to see how it all works out over time. As someone who does some embedded Rust, I'm excited that it might soon be reasonable for me to use core::format_args in my tiny programs. As someone who is concerned about the performance of Rust formatted IO (fasthello, rust-nonstdio), I'm excited that it might soon be more reasonable to use it in performance situations.

  4. kurtlawrence commented on Dec 10, 2023

    @kurtlawrence

    Great read and fantastic work!

  5. Swatinem commented on Dec 17, 2023

    @Swatinem

    Thanks a lot for the work and the great blog post.

    I wonder if it would be possible to avoid using format_args! at all in more places.
    In particular, I often find myself implementing custom formatting logic where all I want to do is to append a Display type to a string buffer.

    Ideally, I would like to have something like fmt::Write::write_display (similar to fmt::Write::write_str) and String::push_display (similar to String::push_str).

    Right now, I believe the only way to do this is via write! (or write_fmt(format_args!(…))), which I believe is just an expansion of the former.

    I was surprised to read the code snippet you gave in the "Closures" section, as Display::fmt(&display_ty, Formatter::new(w))?; is pretty much what I was looking for.
    I thought I was completely missing Formatter::new, but then I tried looking it up and found out that it does not actually exist ;-)

    It would be really nice if one of these solutions existed though, as I believe in lots of cases, usage of format_args! could be completely eliminated.

  6. jabdoa2 commented on Dec 25, 2023

    @jabdoa2

    Great writeup and great work! Would it be possible to use your closure approach only in the release profile (or above a certain opt level)? Would that enable fast debug builds and fast runtime? Or is the compile time blow up too bad even for release builds?

  7. qrilka commented on Apr 18, 2024

    @qrilka

    Typo in the pose:

    ...the compiles is able to efficiently optimize

    I guess it should be s/compiles/compiler/

  8. miaomiao1992 commented on Apr 28, 2024

    @miaomiao1992

    very nice article!

  9. b-guild commented on Sep 17, 2025

    @b-guild

    Maybe instead of expanding format_args!("Hello {}\n{:#}!") to

    fmt::Arguments::new(|w| {
        w.write_str("Hello ")?;
        Display::fmt(&arg1, Formatter::new(w))?;
        w.write_str("\n")?;
        Display::fmt(&arg2, Formatter::new(w).alternate(true))?;
        w.write_str("!")?;
        Ok(())
    })

    How about expanding write!(w,"Hello {}\n{:#}!",a,b)? to

    w.write_str("Hello ")?;
    Display::fmt(&a, Formatter::new(&w))?;
    w.write_str("\n")?;
    Display::fmt(&b, Formatter::new(&w).alternate(true))?;
    w.write_str("!")?;

    That's what I always wish would happen through compiler magic whenever I use write!.

  10. bluebear94 commented on Dec 27, 2025

    @bluebear94

    I wonder how the “list of instructions” method would do with a byte-level representation. I initially thought of using the UTF-8 invalid bytes 0xFE and 0xFF to represent placeholder-related instructions, but that requires us to search for these bytes in write_fmt before calling write_str. Instead, perhaps literal pieces could be prefixed with a length (possibly using a variable-length encoding).

  11. diksipav commented on Apr 4, 2026

    @diksipav

    Hey, I believe that this code

    args: &[
            fmt::Argument::new(&a, Debug::fmt),
            fmt::Argument::new(&b, Debug::fmt),
            fmt::Argument::new(&c, Debug::fmt),
            fmt::Argument::new(&d, Debug::fmt),
            fmt::Argument::new(&e, Debug::fmt),
            fmt::Argument::new(&f, Debug::fmt),
            fmt::Argument::new(&g, Debug::fmt),
        ],
    

    should use Display::fmt in the "Structure Size" section? : )

    Also here:
    "In an ideal world, fmt::Arguments could just be two pointers in size: one pointer to all the static data (string pieces, placeholders, and function pointers), and one pointer to the args array only containing the pointers to the arguments. For our example, this adds up to a total of only 4 pointers to be written. 75% savings!" I don't get how is this 4 pointers, I'd say it is 1 pointer to static data and one wide pointer to args (3 words in total), where args array has 3 pointers to arguments so in total is 6 words. Probably I'm missing sth.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions