2020-06-19 02:56:06 +00:00
|
|
|
use std::fmt;
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct MinMax(i64, i64);
|
|
|
|
|
|
|
|
impl fmt::Display for MinMax {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "({}, {})", self.0, self.1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct Point2D {
|
|
|
|
x: f64,
|
|
|
|
y: f64,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Point2D {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "x: {}, y: {}", self.x, self.y)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-10 14:56:36 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
struct Complex {
|
|
|
|
real: f64,
|
|
|
|
imag: f64,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Complex {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "{} + {}i", self.real, self.imag)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-19 02:56:06 +00:00
|
|
|
fn main() {
|
|
|
|
let minmax = MinMax(0, 14);
|
|
|
|
|
|
|
|
println!("Compare structures:");
|
|
|
|
println!("Display: {}", minmax);
|
|
|
|
println!("Debug: {:?}", minmax);
|
|
|
|
|
|
|
|
let big_range = MinMax(-300, 300);
|
|
|
|
let small_range = MinMax(-3, 3);
|
|
|
|
|
|
|
|
println!(
|
|
|
|
"The big range is {big} and the small is {small}",
|
|
|
|
small = small_range,
|
|
|
|
big = big_range
|
|
|
|
);
|
|
|
|
|
|
|
|
let point = Point2D { x: 3.3, y: 7.2 };
|
|
|
|
|
|
|
|
println!("Compare points:");
|
|
|
|
println!("Display: {}", point);
|
|
|
|
println!("Debug: {:?}", point);
|
2021-09-10 14:56:36 +00:00
|
|
|
|
|
|
|
let cmplx = Complex {
|
|
|
|
real: 3.3,
|
|
|
|
imag: 7.2,
|
|
|
|
};
|
|
|
|
|
|
|
|
println!("Compare complexes:");
|
|
|
|
println!("Display: {}", cmplx);
|
|
|
|
println!("Debug: {:?}", cmplx);
|
2020-06-19 02:56:06 +00:00
|
|
|
}
|