Created
September 7, 2019 23:22
-
-
Save BartMassey/6023817d3fcb91a1547354f37098b679 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::fmt::{self, Display, Formatter}; | |
use std::io::{self, Write}; | |
use tabwriter::TabWriter; | |
struct Table([[usize; 2]; 2]); | |
impl Display for Table { | |
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { | |
let write_table = || -> io::Result<Vec<u8>> { | |
let table = Vec::new(); | |
let mut tf = TabWriter::new(table).padding(1); | |
for r in self.0.iter() { | |
let mut ci = r.iter(); | |
if let Some(c0) = ci.next() { | |
write!(tf, "{}", c0)?; | |
for c in ci { | |
write!(tf, "\t{}", c)?; | |
} | |
} | |
writeln!(tf)?; | |
} | |
tf.flush().unwrap(); | |
Ok(tf.into_inner().unwrap()) | |
}; | |
let table = write_table().or(Err(fmt::Error))?; | |
write!(f, "{}", std::str::from_utf8(&table).unwrap()) | |
} | |
} | |
fn main() { | |
let table = Table([[0, 1], [2, 3]]); | |
print!("{}", table); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment