pub fn timeout<F: Future>(fut: F, timeout: Duration) -> Timeout<F> ⓘ
Expand description
Run the future with a timeout, cancelling it if it doesn’t complete in time.
§Example
Try to connect to example.com:80
with a 10 second timeout
use std::time::Duration;
use std::net::{ToSocketAddrs, TcpStream};
use local_runtime::{io::Async, time::timeout};
let addr = "example.com:80".to_socket_addrs().unwrap().next().unwrap();
local_runtime::block_on(async {
match timeout(async {
Async::<TcpStream>::connect(addr).await.unwrap()
}, Duration::from_secs(10)).await {
Ok(stream) => println!("Connected"),
Err(_) => eprintln!("Timed out")
}
});