refactor: changed Display implementation
This commit is contained in:
2
Cargo.lock
generated
2
Cargo.lock
generated
@@ -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"
|
||||||
|
|||||||
33
src/lib.rs
33
src/lib.rs
@@ -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(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user