フォーマット
これまで、文字列がどのようにフォーマットされるかは フォーマット文字列 によって決まるということを見てきました 。
format!("{}", foo)->"3735928559"format!("0x{:X}", foo)->"0xDEADBEEF"format!("0o{:o}", foo)->"0o33653337357"
ここでは(foo)という単一の変数がX、o、 指定なし 、という様々な 引数タイプ に応じてフォーマットされています。
フォーマットの機能はそれぞれの引数タイプごとに個別のトレイトを用いて実装されています。最も一般的なトレイトは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のドキュメンテーションから参照できます。
演習
上にあるソースコード中のColorという構造体のためのfmt::Displayトレイトの実装を追加しましょう。出力は以下のようになるはずです。
RGB (128, 255, 90) 0x80FF5A
RGB (0, 3, 254) 0x0003FE
RGB (0, 0, 0) 0x000000
Two hints if you get stuck:
- それぞれの色を2回以上記述する必要があるかもしれません。
- 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.