Last active
September 28, 2025 22:57
-
-
Save szabgab/cccc47e11492f4576aa0567731c224b6 to your computer and use it in GitHub Desktop.
tail -f in rust
This file contains hidden or 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, path::PathBuf, time::Duration}; | |
| use tokio::fs::File; | |
| use tokio::io::{AsyncBufReadExt, AsyncSeekExt, BufReader, SeekFrom}; | |
| use tokio::time::sleep; | |
| #[tokio::main] | |
| async fn main() { | |
| let filename = get_filename(); | |
| tail_file(filename).await; | |
| } | |
| async fn tail_file(filename: PathBuf) { | |
| let mut file = File::open(&filename).await.expect("Failed to open file"); | |
| file.seek(SeekFrom::End(0)).await.expect("Failed to seek"); | |
| loop { | |
| sleep(Duration::from_millis(500)).await; | |
| let mut reader = BufReader::new(&mut file); | |
| let mut buf = String::new(); | |
| while reader | |
| .read_line(&mut buf) | |
| .await | |
| .expect("Failed to read line") | |
| > 0 | |
| { | |
| print!("{}", buf); | |
| buf.clear(); | |
| } | |
| } | |
| } | |
| fn get_filename() -> PathBuf { | |
| let args: Vec<String> = env::args().collect(); | |
| if args.len() != 2 { | |
| eprintln!("Usage: {} <filename>", args[0]); | |
| std::process::exit(1); | |
| } | |
| PathBuf::from(&args[1]) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi.
Nice little tail implementation!
A quick suggestion:
Your code uses std::fs::File and blocking reads on the tokio runtime, which can block the runtime and hurt concurrency.
You can use the Tokio's async file APIs (tokio::fs::File + tokio::io::AsyncBufReadExt / AsyncSeekExt) so the program doesn't block other async tasks while waiting for I/O.
Here is a small async rewrite you can use as a drop-in suggestion: