expression

Reset a pinned timer

A reusable Sleep is pinned before resetting its deadline. This allows changing the timer in place without moving its internal future state. Tokio is an external dependency; see the proposed Cargo manifest. Expected stdout (not executed): true true Common mistake: Attempting to move the pinned Sleep out of its binding.

Formal Syntax & Structure

rust syntaxrust
sleep.as_mut().reset(deadline)

Working Example

Example Snippetrust
#[tokio::main(flavor = "current_thread", start_paused = true)]async fn main() {    use tokio::time::{sleep, Duration, Instant};    let timer = sleep(Duration::from_secs(20));    tokio::pin!(timer);    let target = Instant::now() + Duration::from_secs(1);    timer.as_mut().reset(target);    println!("{}", timer.deadline() == target);    timer.as_mut().await;    println!("{}", timer.is_elapsed());}
Rust Reference