Rust does not support traditional OS signal handlers because its memory safety guarantees conflict with the asynchronous nature of signals. You must use external crates like signal-hook to register callbacks that safely handle signals without violating the borrow checker.
use signal_hook::consts::signal::SIGINT;
use signal_hook::iterator::Signals;
fn main() {
let mut signals = Signals::new(&[SIGINT]).unwrap();
for _signal in signals.forever() {
println!("Received SIGINT, exiting gracefully.");
break;
}
}
Add signal-hook = "0.3" to your Cargo.toml dependencies.