Skip to content

Instantly share code, notes, and snippets.

@skorotkiewicz
Created May 14, 2026 16:57
Show Gist options
  • Select an option

  • Save skorotkiewicz/474860a79dc76e0a7efb6be7bdfdd96c to your computer and use it in GitHub Desktop.

Select an option

Save skorotkiewicz/474860a79dc76e0a7efb6be7bdfdd96c to your computer and use it in GitHub Desktop.
Continuously reads stdin from own stdout
use std::{
io::{self, BufRead, Write},
thread,
time::{Duration, SystemTime, UNIX_EPOCH},
};
fn main() {
// Spawn a thread that continuously reads stdin
thread::spawn(|| {
let stdin = io::stdin();
for line in stdin.lock().lines() {
match line {
Ok(text) => eprintln!("stdin: {}", text),
Err(_) => break,
}
}
});
let stdout = io::stdout();
let mut out = stdout.lock();
loop {
let now = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_millis();
writeln!(out, "{}", now).unwrap();
out.flush().unwrap();
thread::sleep(Duration::from_millis(1000));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment