Skip to content

Instantly share code, notes, and snippets.

@alex-pat
Created June 4, 2018 10:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alex-pat/4094125e3ce3a1e05bc77d53c927ca2d to your computer and use it in GitHub Desktop.
Save alex-pat/4094125e3ce3a1e05bc77d53c927ca2d to your computer and use it in GitHub Desktop.
Little cat(1) on Rust
use std::{
env::args_os,
ffi::OsString,
fs::File,
io::{Read, Write},
};
const PAGE_SIZE: usize = 4096;
fn main() {
for path in args_os().skip(1) {
if let Err(err) = cat(&path) {
eprintln!("rat: {:?}: {}", path, err);
}
}
}
fn cat(path: &OsString) -> std::io::Result<()> {
let mut file = File::open(path)?;
let mut buf = [0u8; PAGE_SIZE];
let mut stdout = std::io::stdout();
loop {
match file.read(&mut buf)? {
0 => break,
size => stdout.write_all(&buf[..size])?,
}
}
Ok(())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment