Skip to content

Instantly share code, notes, and snippets.

@franciscoj
Created April 18, 2020 21:08
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 franciscoj/8e44fc1bcf0e893522b6778b303d03fa to your computer and use it in GitHub Desktop.
Save franciscoj/8e44fc1bcf0e893522b6778b303d03fa to your computer and use it in GitHub Desktop.
Tail but in aprox 30 lines of Rust
// This is not at all like tail. Will fail in many scenarios but it was funny to write it :)
use std::fs::File;
use std::io::prelude::*;
use std::io::{BufReader, SeekFrom};
use std::thread::sleep;
use std::time::Duration;
fn main() {
let file_name = "some_file.log";
let file = File::open(file_name.clone()).unwrap();
let mut reader = BufReader::new(file);
reader.seek(SeekFrom::End(0)).unwrap();
loop {
let mut line = String::new();
let res = reader.read_line(&mut line);
match res {
Ok(len) => {
if len > 0 {
println!("=> {}", line.replace("\n", ""));
line.clear();
} else {
sleep(Duration::new(1, 0));
}
}
Err(err) => {
println!("=> ERR: {}", err)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment