▲ 2 r/rust
New crate announcement: agp (actual good printing), because default map debug formatting shouldn't be a nightmare to work with
ever struggled with the pain of {:?} on hashmaps? sure, i guess {:#?} is a bit better, but all it does is just not make it a one-line blob.
agp (actual good printing) has come to save you!
agp is a lightweight, zero-dependency crate designed to make quick terminal debugging of collections readable without the intense vertical bloat of {:#?} or the unreadable wall-of-text of {:?}.
examples
use std::collections::HashMap;
use agp::ActualGoodPrinting;
fn main() {
let mut map = HashMap::new();
map.insert("status", "ok");
map.insert("code", "200");
map.agp();
// output (order may vary):
// status: ok
// code: 200
}
to return it, do the same but swap .agp() for .return_agp():
use std::collections::HashMap;
use agp::ActualGoodPrinting;
fn main() {
let mut map = HashMap::new();
map.insert("status", "ok");
map.insert("code", "200");
let good_map = map.return_agp();
println!("{good_map}");
}
how to implement your own agp
use agp::ActualGoodPrinting;
use std::collections::BTreeMap;
use std::fmt::Display;
use std::fmt::Write;
impl<K, V> ActualGoodPrinting for BTreeMap<K, V> // replace this with your own
where:
K: Display + Ord,
V: Display,
{
fn agp(&self) {
for (k, v) in self {
println!("{k}: {v}");
}
}
fn return_agp(&self) -> String {
let mut result = String::with_capacity(self.len() * 32);
for (k, v) in self {
writeln!(result, "{k}: {v}").unwrap();
}
result
}
}
pls do not impl for HashMap or Vec tho, as we already have that ([T] for vec and standard arrays)
u/DimaTF2Player — 1 day ago