How to make a dynamic number counter so that when you press the up arrow, a number is added and when you press the down arrow, a number is subtracted?
I asked myself that question and proceeded to try to solve that algorithm in my mind first and then in code, here is the result.
Let's add the keyboard handler k_board
cargo add k_board
Let's add the program logic:
use k_board::{Keyboard, Keys}; fn main() { let mut number: i8 = 0; print_number(&mut number, 0); for key in Keyboard::new() { match key { Keys::Up => print_number(&mut number, 1), Keys::Down => print_number(&mut number, -1), Keys::Enter => break, _ => {} } } } fn print_number(number: &mut i8, operation: i8) { std::process::Command::new("clear").status().unwrap(); *number += operation; println!("{}", number); }
See you soon love ❤️
Top comments (0)