Skip to content

Commit 88f5ca1

Browse files
committed
Implement Display for Grid
1 parent e0e4919 commit 88f5ca1

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

src/grid/mod.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
use std::{io, ops::Deref};
1+
use std::{
2+
fmt::{self, Display, Write},
3+
io,
4+
ops::Deref,
5+
};
26

37
#[derive(Debug)]
48
pub struct Grid {
@@ -129,6 +133,15 @@ impl Deref for Grid {
129133
}
130134
}
131135

136+
impl Display for Grid {
137+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
138+
self.grid.chunks_exact(self.width).try_for_each(|line| {
139+
let buffer: String = line.iter().collect();
140+
f.write_str(&buffer).and_then(|_| f.write_char('\n'))
141+
})
142+
}
143+
}
144+
132145
pub trait Neighbors: Iterator<Item = usize> {
133146
fn north(&self) -> Option<usize>;
134147
fn east(&self) -> Option<usize>;

0 commit comments

Comments
 (0)