Compare commits
2 Commits
82ed5d328d
...
60da2e20dc
Author | SHA1 | Date | |
---|---|---|---|
60da2e20dc | |||
8f891c1e73 |
87
rustbyexample/hello/print_fmt.rs
Normal file
87
rustbyexample/hello/print_fmt.rs
Normal file
@ -0,0 +1,87 @@
|
||||
use std::fmt::{self, Display, Formatter};
|
||||
|
||||
struct City {
|
||||
name: &'static str,
|
||||
lat: f32,
|
||||
lon: f32,
|
||||
}
|
||||
|
||||
impl Display for City {
|
||||
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!(
|
||||
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 ({}, {}, {}) 0x{:02X}{:02X}{:02X}",
|
||||
self.red, self.green, self.blue, self.red, self.green, 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()
|
||||
{
|
||||
println!("{}", *color);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user