diff --git a/rustbyexample/0122/Cargo.lock b/rustbyexample/0122/Cargo.lock new file mode 100644 index 0000000..05769cc --- /dev/null +++ b/rustbyexample/0122/Cargo.lock @@ -0,0 +1,4 @@ +[root] +name = "0122" +version = "0.1.0" + diff --git a/rustbyexample/0122/Cargo.toml b/rustbyexample/0122/Cargo.toml new file mode 100644 index 0000000..8fd0278 --- /dev/null +++ b/rustbyexample/0122/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "0122" +version = "0.1.0" +authors = ["Dan Buch "] + +[dependencies] diff --git a/rustbyexample/0122/src/main.rs b/rustbyexample/0122/src/main.rs new file mode 100644 index 0000000..6d3e2f8 --- /dev/null +++ b/rustbyexample/0122/src/main.rs @@ -0,0 +1,43 @@ +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 Point2 { + x: f64, + y: f64, +} + +impl fmt::Display for Point2 { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "x: {}, y: {}", self.x, self.y) + } +} + +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 range is {small}", + small=small_range, + big=big_range); + + let point = Point2 { x: 3.3, y: 7.2 }; + + println!("Compare points:"); + println!("Display: {}", point); + println!("Debug: {:?}", point); +} diff --git a/rustbyexample/0122/src/structure.rs b/rustbyexample/0122/src/structure.rs new file mode 100644 index 0000000..5602401 --- /dev/null +++ b/rustbyexample/0122/src/structure.rs @@ -0,0 +1,9 @@ +use std::fmt; + +struct Structure(i32); + +impl fmt::Display for Structure { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}", self.0); + } +}