Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Process and Ctrl-c

use tokio::select;

#[tokio::main]
async fn main() {
    let mut count = 0;
    loop {
        select! {
            _ = wait_a_bit() => println!("Waited a bit!"),
            _ = tokio::signal::ctrl_c() => {
                    count += 1;
                    if count >= 2 {
                        println!("Exiting...");
                        break;
                    }
                    println!("Received Ctrl+C! Press Ctrl-C again if you'd like to quit?");
            }
        }
    }
}

async fn wait_a_bit() {
    println!("Waiting a bit...");
    tokio::time::sleep(std::time::Duration::from_secs(1)).await;
}




[package]
name = "demo"
version = "0.1.0"
edition = "2024"

[dependencies]
tokio = { version = "1.47.1", features = ["full"] }