Skip to content

Commit a5fec39

Browse files
authored
Merge pull request #1951 from olp-cs/fmt-hints
Improve the hints in `fmt` (1.2.3. Formatting); mention type casting
2 parents adc1f3b + 02cbf78 commit a5fec39

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

src/hello/print/fmt.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,18 @@ RGB (0, 3, 254) 0x0003FE
7878
RGB (0, 0, 0) 0x000000
7979
```
8080

81-
Three hints if you get stuck:
81+
Two hints if you get stuck:
8282

83-
* The formula for calculating a color in the RGB color space is:
84-
`RGB = (R*65536)+(G*256)+B , (when R is RED, G is GREEN and B is BLUE)`.
85-
For more see [RGB color format & calculation][rgb_color].
8683
* You [may need to list each color more than once][named_parameters].
8784
* You can [pad with zeros to a width of 2][fmt_width] with `:0>2`.
85+
For hexadecimals, you can use `:02X`.
86+
87+
Bonus:
88+
89+
* If you would like to experiment with [type casting][type_casting] in advance,
90+
the formula for [calculating a color in the RGB color space][rgb_color] is
91+
`RGB = (R * 65_536) + (G * 256) + B`, where `R is RED, G is GREEN, and B is BLUE`.
92+
An unsigned 8-bit integer (`u8`) can only hold numbers up to 255. To cast `u8` to `u32`, you can write `variable_name as u32`.
8893

8994
### See also:
9095

@@ -96,3 +101,4 @@ For more see [RGB color format & calculation][rgb_color].
96101
[fmt]: https://doc.rust-lang.org/std/fmt/
97102
[fmt_traits]: https://doc.rust-lang.org/std/fmt/#formatting-traits
98103
[fmt_width]: https://doc.rust-lang.org/std/fmt/#width
104+
[type_casting]: ../../types/cast.md

0 commit comments

Comments
 (0)