inner_function/
inner_function.rs

1use std::{thread, time::Duration};
2
3fn main() {
4    let on_timeout = || {
5        println!("timeout!");
6    };
7
8    // Leaking this inner function to make its lifetime as 'static.
9    // https://doc.rust-lang.org/std/boxed/struct.Box.html#method.leak
10    let static_on_timeout = Box::leak(Box::new(on_timeout));
11
12    let cancel = spawn_timeout::spawn_timeout(static_on_timeout, Duration::from_secs(3));
13    let _ = spawn_timeout::spawn_timeout(static_on_timeout, Duration::from_secs(3));
14
15    // Waiting before cancelling this instance of spawn_timeout.
16    thread::sleep(Duration::from_millis(1500));
17
18    cancel();
19
20    println!("The first instance of spawn_timeout has been succesfully stopped");
21
22    // Sleeping for a long time for the sake of this example.
23    thread::sleep(Duration::from_millis(u64::MAX));
24}