格式化
我们已经看到,格式化是通过一个_格式字符串_来指定的:
format!("{}", foo)->"3735928559"format!("0x{:X}", foo)->"0xDEADBEEF"format!("0o{:o}", foo)->"0o33653337357"
同一个变量(foo)可以根据使用的参数类型而有不同的格式化方式:X、o 或未指定。
这种格式化功能是通过 trait 实现的,每种参数类型都对应一个 trait。最常用的格式化 trait 是 Display,它处理参数类型未指定的情况,例如 {}。
use std::fmt::{self, Formatter, Display};
struct City {
name: &'static str,
// 纬度
lat: f32,
// 经度
lon: f32,
}
impl Display for City {
// `f` 是一个缓冲区,此方法必须将格式化的字符串写入其中。
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!` 类似于 `format!`,但它会将格式化后的字符串
// 写入一个缓冲区(第一个参数)。
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,
}
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 },
] {
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 },
] {
// 一旦你为 fmt::Display 添加了实现,就把这里改成使用 {}。
println!("{:?}", color);
}
}
你可以在 std::fmt 文档中查看格式化 trait 的完整列表及其参数类型。
练习
为上面的 Color 结构体实现 fmt::Display trait,使输出显示如下:
RGB (128, 255, 90) 0x80FF5A
RGB (0, 3, 254) 0x0003FE
RGB (0, 0, 0) 0x000000
Two hints if you get stuck:
- 你可能需要多次列出每种颜色。
- You can pad with zeros to a width of 2 with
:0>2. For hexadecimals, you can use:02X.
Bonus:
- If you would like to experiment with type casting in advance, the formula for calculating a color in the RGB color space is
RGB = (R * 65_536) + (G * 256) + B, whereR is RED, G is GREEN, and B is BLUE. An unsigned 8-bit integer (u8) can only hold numbers up to 255. To castu8tou32, you can writevariable_name as u32.