refactor: changed Display implementation
This commit is contained in:
2
Cargo.lock
generated
2
Cargo.lock
generated
@@ -5,7 +5,7 @@ version = 4
|
||||
[[package]]
|
||||
name = "fractions"
|
||||
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]]
|
||||
name = "matrix"
|
||||
|
||||
33
src/lib.rs
33
src/lib.rs
@@ -580,19 +580,30 @@ impl Mul for Matrix {
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for Matrix {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
let mut display = String::new();
|
||||
let mut index = 0;
|
||||
for _i in 0..self.columns {
|
||||
display += "{";
|
||||
for _k in 0..self.rows {
|
||||
display += &format!(" {},", self.data[index]);
|
||||
index += 1;
|
||||
impl fmt::Display for Matrix {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
let strings: Vec<String> = self.data.iter().map(|x| x.to_string()).collect();
|
||||
|
||||
let max_width = strings.iter().map(|s| s.len()).max().unwrap_or(0);
|
||||
|
||||
for r in 0..self.rows {
|
||||
write!(f, "{{ ")?;
|
||||
|
||||
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";
|
||||
}
|
||||
write!(f, "{}", display)
|
||||
|
||||
writeln!(f, " }}")?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user