Skip to content

Rust by Example seems to show incorrect hint for Formatted Print #1459

@cod-eric

Description

@cod-eric

What needs to be fixed?

Disclaimer: I am a complete newbie to Rust, so I might be missing something.
I am following the Rust by Example Guide and got stuck on 1.2.3 - Hello World/Formatted print/Formatting, because the HEX code I was supposed to print out was padded using spaces instead of 0s, when following the second hint at the bottom of the page: You can pad with zeros to a with of 2 with :02.
After some googling, I found out that it actually is :0>2 instead of :02.
As said before, I might be missing something, but in my code (which I'll include below), :02 does not work.

#[derive(Debug)]
struct Color {
    red: u8,
    green: u8,
    blue: u8,
}

impl Display for Color {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        let red_hex = format!("{:X}", self.red);
        let green_hex = format!("{:X}", self.green);
        let blue_hex = format!("{:X}", self.blue);

        write!(f, "RGB ({red}, {green}, {blue}) 0x{redHex:0>2}{greenHex:0>2}{blueHex:0>2}",
                                red = self.red,
                                green = self.green,
                                blue = self.blue,
                                redHex = red_hex,
                                greenHex = green_hex,
                                blueHex = blue_hex)
    }

Page(s) Affected

Rust by Example, 1.2.3: Formatting (https://doc.rust-lang.org/rust-by-example/hello/print/fmt.html)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions