There has been a buzz about Rust lang for a while so today I decided to embark on the journey to learn Rust lang with a plan to spend one hour every day on the official website https://doc.rust-lang.org/rust-by-example/.
Most of the explanations and activities were fairly straightforward requiring you to refer to this documentation https://doc.rust-lang.org/std/fmt/.
The only question which I felt left me a bit lost was the last one where you had to display a number in Hex and with padding. After going through the docs and realising that the following message actually meant that the format syntax was wrong:
Compiling playground v0.0.1 (/playground)
error: invalid format string: expected `'}'`, found `'0'`
--> src/main.rs:33:57
|
33 | write!(f, "RGB ({red}, {green}, {blue}) 0x{red:X02}{green:02X}{blue:02X}",
| - ^ expected `}` in format string
| |
| because of this opening brace
|
= note: if you intended to print `{`, you can escape it using `{{`
error: aborting due to previous error
error: could not compile `playground`
To learn more, run the command again with --verbose.
I went ahead to read the syntax part of the documentation at https://doc.rust-lang.org/std/fmt/#syntax
In there I found out that the order in which the format specifications is given is important. For example the above error happens because hex format spec should be after the padding i.e {red:X02} -> {red:02X}.
If you are curious the correct final code looks as follows:
use std::fmt::{self, Formatter, Display};
struct City {
name: &'static str,
// Latitude
lat: f32,
// Longitude
lon: f32,
}
impl Display for City {
// `f` is a buffer, and this method must write the formatted string into it
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
let lat_c = if self.lat >= 0.0 { 'N' } else { 'S' };
let lon_c = if self.lon >= 0.0 { 'E' } else { 'W' };
// `write!` is like `format!`, but it will write the formatted string
// into a buffer (the first argument)
write!(f, "{}: {:.3}°{} {:.3}°{}",
self.name, self.lat.abs(), lat_c, self.lon.abs(), lon_c)
}
}
#[derive(Debug)]
struct Color {
red: u8,
green: u8,
blue: u8,
}
impl Display for Color {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "RGB ({red}, {green}, {blue}) 0x{red:02X}{green:02X}{blue:02X}",
red=self.red, green=self.green, blue=self.blue)
}
}
fn main() {
for city in [
City { name: "Dublin", lat: 53.347778, lon: -6.259722 },
City { name: "Oslo", lat: 59.95, lon: 10.75 },
City { name: "Vancouver", lat: 49.25, lon: -123.1 },
].iter() {
println!("{}", *city);
}
for color in [
Color { red: 128, green: 255, blue: 90 },
Color { red: 0, green: 3, blue: 254 },
Color { red: 0, green: 0, blue: 0 },
].iter() {
// Switch this to use {} once you've added an implementation
// for fmt::Display.
println!("{:}", *color);
}
}
Hope this helped you if you were stuck in Rust-Lang formatting question. I am also streaming learning Rust live at https://www.twitch.tv/lougarou_ everyday at 8pm GMT+4 if you are interested to learn together with me.