Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // type something and hit enter
- // from https://stackoverflow.com/a/55201400/5066426
- use std::io;
- use std::sync::mpsc;
- use std::sync::mpsc::Receiver;
- use std::sync::mpsc::TryRecvError;
- use std::{thread, time};
- fn main() {
- let stdin_channel = spawn_stdin_channel();
- loop {
- match stdin_channel.try_recv() {
- Ok(key) => println!("Received: {}", key),
- Err(TryRecvError::Empty) => println!("Channel empty"),
- Err(TryRecvError::Disconnected) => panic!("Channel disconnected"),
- }
- sleep(1000);
- }
- }
- fn spawn_stdin_channel() -> Receiver<String> {
- let (tx, rx) = mpsc::channel::<String>();
- thread::spawn(move || loop {
- let mut buffer = String::new();
- io::stdin().read_line(&mut buffer).unwrap();
- tx.send(buffer).unwrap();
- });
- rx
- }
- fn sleep(millis: u64) {
- let duration = time::Duration::from_millis(millis);
- thread::sleep(duration);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement