feat: 1.2.2: Display /w activity
This commit is contained in:
parent
2c8e209c61
commit
ec8764f48d
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
*/target/
|
||||||
|
target/
|
||||||
7
1-hello-world/formatted-printing/display/Cargo.lock
generated
Normal file
7
1-hello-world/formatted-printing/display/Cargo.lock
generated
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 4
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "deisplay"
|
||||||
|
version = "0.1.0"
|
||||||
@ -1,3 +1,111 @@
|
|||||||
fn main() {
|
// Import (via `use`) the `fmt` module to make it available.
|
||||||
println!("Hello, world!");
|
use std::fmt;
|
||||||
|
|
||||||
|
// Define a structure for which `fmt::Display` will be implemented. This is
|
||||||
|
// a tuple struct named `Structure` that contains an `i32`.
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct Structure(i32);
|
||||||
|
|
||||||
|
// To use the `{}` marker, the trait `fmt::Display` must be implemented
|
||||||
|
// manually for the type.
|
||||||
|
impl fmt::Display for Structure {
|
||||||
|
// This trait requires `fmt` with this exact signature.
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
// Write strictly the first element into the supplied output
|
||||||
|
// stream: `f`. Returns `fmt::Result` which indicates whether the
|
||||||
|
// operation succeeded or failed. Note that `write!` uses syntax which
|
||||||
|
// is very similar to `println!`.
|
||||||
|
write!(f, "S[i32] => {}", self.0)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ======================================================
|
||||||
|
|
||||||
|
// A structure holding two numbers. `Debug` will be derived so the results can
|
||||||
|
// be contrasted with `Display`.
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct MinMax(i64, i64);
|
||||||
|
|
||||||
|
// Implement `Display` for `MinMax`.
|
||||||
|
impl fmt::Display for MinMax {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
// Use `self.number` to refer to each positional data point.
|
||||||
|
write!(f, "({}, {})", self.0, self.1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Define a structure where the fields are nameable for comparison.
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct Point2D {
|
||||||
|
x: f64,
|
||||||
|
y: f64,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Similarly, implement `Display` for `Point2D`.
|
||||||
|
impl fmt::Display for Point2D {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
// Customize so only `x` and `y` are denoted.
|
||||||
|
write!(f, "x: {}, y: {}", self.x, self.y)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ======================================================
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct Complex {
|
||||||
|
r: f64,
|
||||||
|
i: f64,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for Complex {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
if self.i < 0_f64 {
|
||||||
|
return write!(f, "{r} | - {i}i", r = self.r, i = self.i.abs());
|
||||||
|
}
|
||||||
|
return write!(f, "{r} | + {i}i", r = self.r, i = self.i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// Note that we use `{}` - which is "normal displaying"
|
||||||
|
println!("Structure {} will print!", Structure(7));
|
||||||
|
|
||||||
|
// ======================================================
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
// The following line would not compile: both `Debug` and `Display`
|
||||||
|
// were implemented, but `{:b}` requires `fmt::Binary` to be
|
||||||
|
// implemented, which it hasn't been for `Point2D`.
|
||||||
|
// println!("What does Point2D look like in binary: {:b}?", point);
|
||||||
|
//
|
||||||
|
let positive_complex = Complex{ r : 3.3_f64, i : 7.2_f64 };
|
||||||
|
let negative_complex = Complex{ r : 3.3_f64, i : -7.2_f64 };
|
||||||
|
|
||||||
|
println!("Compare complex:");
|
||||||
|
println!("Display: {}", positive_complex);
|
||||||
|
println!("Debug: {:?}", positive_complex);
|
||||||
|
|
||||||
|
println!("Compare complex:");
|
||||||
|
println!("Display: {}", negative_complex);
|
||||||
|
println!("Debug: {:?}", negative_complex);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -0,0 +1 @@
|
|||||||
|
{"rustc_fingerprint":14802351894921902018,"outputs":{"1847801304416329599":{"success":true,"status":"","code":0,"stdout":"rustc 1.98.1 (48a229cea 2026-09-01)\nbinary: rustc\ncommit-hash: 48a229ceaefd4985c50990b14116b6d856af0985\ncommit-date: 2026-09-01\nhost: x86_64-unknown-linux-gnu\nrelease: 1.98.1\nLLVM version: 22.1.8\n","stderr":""},"7971740275564407648":{"success":true,"status":"","code":0,"stdout":"___\nlib___.rlib\nlib___.so\nlib___.so\nlib___.a\nlib___.so\n/home/nak/.rustup/toolchains/stable-x86_64-unknown-linux-gnu\noff\npacked\nunpacked\n___\ndebug_assertions\npanic=\"unwind\"\nproc_macro\ntarget_abi=\"\"\ntarget_arch=\"x86_64\"\ntarget_endian=\"little\"\ntarget_env=\"gnu\"\ntarget_family=\"unix\"\ntarget_feature=\"fxsr\"\ntarget_feature=\"sse\"\ntarget_feature=\"sse2\"\ntarget_has_atomic=\"16\"\ntarget_has_atomic=\"32\"\ntarget_has_atomic=\"64\"\ntarget_has_atomic=\"8\"\ntarget_has_atomic=\"ptr\"\ntarget_has_atomic_primitive_alignment=\"16\"\ntarget_has_atomic_primitive_alignment=\"32\"\ntarget_has_atomic_primitive_alignment=\"64\"\ntarget_has_atomic_primitive_alignment=\"8\"\ntarget_has_atomic_primitive_alignment=\"ptr\"\ntarget_os=\"linux\"\ntarget_pointer_width=\"64\"\ntarget_vendor=\"unknown\"\nunix\n","stderr":""}},"successes":{}}
|
||||||
@ -0,0 +1,3 @@
|
|||||||
|
Signature: 8a477f597d28d172789f06886806bc55
|
||||||
|
# This file is a cache directory tag created by cargo.
|
||||||
|
# For information about cache directory tags see https://bford.info/cachedir/
|
||||||
@ -0,0 +1 @@
|
|||||||
|
e0bf6de377ff0135
|
||||||
@ -0,0 +1 @@
|
|||||||
|
{"rustc":1625334936438085654,"features":"[]","declared_features":"[]","target":9871736305712052871,"profile":8731458305071235362,"path":4942398508502643691,"deps":[],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/deisplay-80701de140c8cedd/dep-bin-deisplay","checksum":false}}],"rustflags":[],"config":9396254390672932401,"compile_kind":0}
|
||||||
Binary file not shown.
@ -0,0 +1 @@
|
|||||||
|
This file has an mtime of when this was started.
|
||||||
BIN
1-hello-world/formatted-printing/display/target/debug/deisplay
Executable file
BIN
1-hello-world/formatted-printing/display/target/debug/deisplay
Executable file
Binary file not shown.
@ -0,0 +1 @@
|
|||||||
|
/home/nak/Code/rust/learn-rust/1-hello-world/formatted-printing/display/target/debug/deisplay: /home/nak/Code/rust/learn-rust/1-hello-world/formatted-printing/display/src/main.rs
|
||||||
Binary file not shown.
@ -0,0 +1,5 @@
|
|||||||
|
/home/nak/Code/rust/learn-rust/1-hello-world/formatted-printing/display/target/debug/deps/deisplay-80701de140c8cedd.d: src/main.rs
|
||||||
|
|
||||||
|
/home/nak/Code/rust/learn-rust/1-hello-world/formatted-printing/display/target/debug/deps/deisplay-80701de140c8cedd: src/main.rs
|
||||||
|
|
||||||
|
src/main.rs:
|
||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue
Block a user