Created
May 14, 2026 16:57
-
-
Save skorotkiewicz/474860a79dc76e0a7efb6be7bdfdd96c to your computer and use it in GitHub Desktop.
Continuously reads stdin from own stdout
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::{ | |
| 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