Skip to content

std::fmt needs doc for precision := '*' case #24656

Closed
@pnkfelix

Description

@pnkfelix

I spent a while looking at http://doc.rust-lang.org/nightly/std/fmt/index.html#syntax and comparing it against the rest of the API documentation there, but I saw no discussion of the meaning of the asterisk precision, e.g. what the format string "{:.*}" denotes.

The error message you get when you try to use it incorrectly is pretty inscrutable:

    println!("{:.*}", 0.0);

yields

<std macros>:3:11: 3:36 error: invalid reference to argument `1` (there is 1 argument)
<std macros>:3 print ! ( concat ! ( $ fmt , "\n" ) , $ ( $ arg ) * ) ) ;
                         ^~~~~~~~~~~~~~~~~~~~~~~~~
note: in expansion of format_args!

Manual experimentation seems to indicate that the meaning of {:.*} is that the next two parameters are consumed, where the first represents the fractional precision and the second the value to be printed.

As a concrete example, the following four statements all print the same thing:

fn main() {
    println!("Hello {1} is {2:.0$}", 5, "x", 0.01);
    println!("Hello {0} is {2:.1$}", "x", 5, 0.01);
    println!("Hello {} is {:.*}",    "x", 5, 0.01);
    println!("Hello {} is {2:.*}",   "x", 5, 0.01);
}

namely:

Hello x is 0.01000
Hello x is 0.01000
Hello x is 0.01000
Hello x is 0.01000

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions