Skip to content

std::timer

Waiting: timer.sleep suspends the current task for a Duration. Instant and Duration are the time values it works with.

Import with import std::timer.

fn sleep(d: Duration) -> Unit

Suspends the current task for the given duration. A zero or negative duration returns at once, so timer.sleep(deadline - now) is right when the deadline has passed. A duration past about 292 years, the longest wait the clock counts, waits that long: the sleep ends only when its context is cancelled.

When called inside a concurrent block, cancellation of that block’s context ends the sleep early without producing a user-visible error.

deadline = Instant.from_seconds(0)
assert timer.sleep(Instant.between(Instant.from_seconds(1), deadline)) == Unit