Skip to content

documentation for fmt binary trait: slightly misleading example #116165

Closed
@feored

Description

@feored

Location

https://doc.rust-lang.org/std/fmt/trait.Binary.html

Specifically the last assert in the Examples section:

assert_eq!( format!("l as binary is: {l:#032b}"), "l as binary is: 0b000000000000000000000001101011" );

Summary

This example shows how to format the binary representation of an i32 and was probably meant to display all 32 bits, but actually only displays 30 bits because the alternate flag # adds the prefix 0b to the representation, which is included in the total width of the padding (which is mentioned in passing here: https://doc.rust-lang.org/std/fmt/index.html#sign0 ).

I think it would be useful to mention in that example that the 0b prefix is included in the total width, and I would also suggest changing the example to use {l:#034b} as that is probably what the reader is expecting when displaying a padded i32,

Metadata

Metadata

Assignees

Labels

A-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsT-libsRelevant to the library team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions