refactor: changed Display implementation

This commit is contained in:
2026-04-29 19:48:23 -06:00
parent 384544c5ae
commit 074b1edbb6
2 changed files with 23 additions and 12 deletions

2
Cargo.lock generated
View File

@@ -5,7 +5,7 @@ version = 4
[[package]] [[package]]
name = "fractions" name = "fractions"
version = "0.1.0" version = "0.1.0"
source = "git+https://laentropia-homelab.tail7368da.ts.net/laentropia/Rusty-Fractions.git?branch=main#885bbfeebed047a62ef86eae5bcc44137e2ae127" source = "git+https://laentropia-homelab.tail7368da.ts.net/laentropia/Rusty-Fractions.git?branch=main#0d5511b4ebd5418559bd5227e75e0446ff1a41f3"
[[package]] [[package]]
name = "matrix" name = "matrix"

View File

@@ -580,19 +580,30 @@ impl Mul for Matrix {
} }
} }
impl Display for Matrix { impl fmt::Display for Matrix {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut display = String::new(); let strings: Vec<String> = self.data.iter().map(|x| x.to_string()).collect();
let mut index = 0;
for _i in 0..self.columns { let max_width = strings.iter().map(|s| s.len()).max().unwrap_or(0);
display += "{";
for _k in 0..self.rows { for r in 0..self.rows {
display += &format!(" {},", self.data[index]); write!(f, "{{ ")?;
index += 1;
for c in 0..self.columns {
let idx = r * self.columns + c;
let val = &strings[idx];
write!(f, "{:>width$}", val, width = max_width)?;
if c != self.columns - 1 {
write!(f, ", ")?;
}
} }
display += " }\n";
writeln!(f, " }}")?;
} }
write!(f, "{}", display)
Ok(())
} }
} }