Created
June 4, 2018 10:12
-
-
Save alex-pat/4094125e3ce3a1e05bc77d53c927ca2d to your computer and use it in GitHub Desktop.
Little cat(1) on Rust
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::{ | |
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