use std::io::BufReader; use std::{env, fs::File, io::Read}; fn main() -> Result<(), Box> { const CHUNK_SIZE: usize = 8; let mut file: Box; file = if let Some(filename) = env::args().nth(1) { Box::new(BufReader::new(File::open(&filename)?)) } else { Box::new(BufReader::new(std::io::stdin())) }; let mut chunk = Vec::with_capacity(CHUNK_SIZE); let mut idx = 0; while { file.by_ref() .take(CHUNK_SIZE as u64) .read_to_end(&mut chunk)? > 0 } { // write index print!("{:010}: ", idx * CHUNK_SIZE); idx += 1; // write hex for byte in &chunk { print!("{:02X} ", byte); } // write ascii println!("{}", String::from_utf8_lossy(&chunk).replace('\n', "⏎")); chunk.clear(); } Ok(()) }