Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
pnkfelix opened this issue Apr 21, 2015 · 0 comments · Fixed by #24662
Closed

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

pnkfelix opened this issue Apr 21, 2015 · 0 comments · Fixed by #24662

Comments

@pnkfelix
Copy link
Member

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
steveklabnik added a commit to steveklabnik/rust that referenced this issue Apr 21, 2015
steveklabnik added a commit to steveklabnik/rust that referenced this issue Apr 24, 2015
steveklabnik added a commit to steveklabnik/rust that referenced this issue Apr 24, 2015
steveklabnik added a commit to steveklabnik/rust that referenced this issue Apr 25, 2015
Fixes rust-lang#24656 

r? @pnkfelix 

I just added the examples, but if the wording needs expanded too, let me know what you think should be added :)
steveklabnik added a commit to steveklabnik/rust that referenced this issue Apr 25, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant