Struct std::time::Duration
[−]
[src]
pub struct Duration { // some fields omitted }
A duration type to represent a span of time, typically used for system timeouts.
Each duration is composed of a number of seconds and nanosecond precision. APIs binding a system timeout will typically round up the nanosecond precision if the underlying system does not support that level of precision.
Durations implement many common traits, including Add
, Sub
, and other
ops traits. Currently a duration may only be inspected for its number of
seconds and its nanosecond precision.
Examples
fn main() { use std::time::Duration; let five_seconds = Duration::new(5, 0); let five_seconds_and_five_nanos = five_seconds + Duration::new(0, 5); assert_eq!(five_seconds_and_five_nanos.as_secs(), 5); assert_eq!(five_seconds_and_five_nanos.subsec_nanos(), 5); let ten_millis = Duration::from_millis(10); }use std::time::Duration; let five_seconds = Duration::new(5, 0); let five_seconds_and_five_nanos = five_seconds + Duration::new(0, 5); assert_eq!(five_seconds_and_five_nanos.as_secs(), 5); assert_eq!(five_seconds_and_five_nanos.subsec_nanos(), 5); let ten_millis = Duration::from_millis(10);
Methods
impl Duration
fn new(secs: u64, nanos: u32) -> Duration
Creates a new Duration
from the specified number of seconds and
additional nanosecond precision.
If the nanoseconds is greater than 1 billion (the number of nanoseconds in a second), then it will carry over into the seconds provided.
fn span<F>(f: F) -> Duration where F: FnOnce()
: use std::time::Instant instead
Runs a closure, returning the duration of time it took to run the closure.
fn from_secs(secs: u64) -> Duration
Creates a new Duration
from the specified number of seconds.
fn from_millis(millis: u64) -> Duration
Creates a new Duration
from the specified number of milliseconds.
fn as_secs(&self) -> u64
Returns the number of whole seconds represented by this duration.
The extra precision represented by this duration is ignored (e.g. extra nanoseconds are not represented in the returned value).
fn subsec_nanos(&self) -> u32
Returns the nanosecond precision represented by this duration.
This method does not return the length of the duration when represented by nanoseconds. The returned number always represents a fractional portion of a second (e.g. it is less than one billion).