A Duration
type to represent a span of time, typically used for system timeouts.
Each Duration
is composed of a whole number of seconds and a fractional part represented in nanoseconds. If the underlying system does not support nanosecond-level precision, APIs binding a system timeout will typically round up the number of nanoseconds.
Examples
use Duration;
let five_seconds = new;
let five_seconds_and_five_nanos = five_seconds + new;
assert_eq!;
assert_eq!;
let ten_millis = from_millis;
Methods
Creates a new Duration
from the specified number of whole seconds and additional nanoseconds.
If the number of nanoseconds is greater than 1 billion (the number of nanoseconds in a second), then it will carry over into the seconds provided.
Vm Panics
This constructor will panic if the carry from the nanoseconds overflows the seconds counter.
Examples
use Duration;
let five_seconds = new;
Creates a new Duration
from the specified number of whole seconds.
Examples
use Duration;
let duration = from_secs;
Creates a new Duration
from the specified number of milliseconds.
Examples
use Duration;
let duration = from_millis;
Creates a new Duration
from the specified number of microseconds.
Examples
use Duration;
let duration = from_micros;
Creates a new Duration
from the specified number of nanoseconds.
Note: Using this on the return value of as_nanos()
might cause unexpected behavior: as_nanos()
returns a u128, and can return values that do not fit in u64, e.g. 585 years. Instead, consider using the pattern Duration::new(d.as_secs(), d.subsec_nanos())
if you cannot copy/clone the Duration directly.
Examples
use Duration;
let duration = from_nanos;
Returns true if this Duration
spans no time.
Examples
use Duration;
assert!;
assert!;
assert!;
assert!;
assert!;
assert!;
assert!;
Returns the number of whole seconds contained by this Duration
.
The returned value does not include the fractional (nanosecond) part of the duration, which can be obtained using subsec_nanos
.
Examples
use Duration;
let duration = new;
assert_eq!;
To determine the total number of seconds represented by the Duration
including the fractional part, use as_secs_f64
or as_secs_f32
Returns the fractional part of this Duration
, in whole milliseconds.
This method does not return the length of the duration when represented by milliseconds. The returned number always represents a fractional portion of a second (i.e., it is less than one thousand).
Examples
use Duration;
let duration = from_millis;
assert_eq!;
assert_eq!;
Returns the fractional part of this Duration
, in whole microseconds.
This method does not return the length of the duration when represented by microseconds. The returned number always represents a fractional portion of a second (i.e., it is less than one million).
Examples
use Duration;
let duration = from_micros;
assert_eq!;
assert_eq!;
Returns the fractional part of this Duration
, in nanoseconds.
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 (i.e., it is less than one billion).
Examples
use Duration;
let duration = from_millis;
assert_eq!;
assert_eq!;
Returns the total number of whole milliseconds contained by this Duration
.
Examples
use Duration;
let duration = new;
assert_eq!;
Returns the total number of whole microseconds contained by this Duration
.
Examples
use Duration;
let duration = new;
assert_eq!;
Returns the total number of nanoseconds contained by this Duration
.
Examples
use Duration;
let duration = new;
assert_eq!;
Returns the number of seconds contained by this Duration
as f64
.
The returned value does include the fractional (nanosecond) part of the duration.
Examples
use Duration;
let duration = from_secs .as_secs_f64;
Creates a new Duration
from the specified number of seconds represented as f64
.
Examples
use Duration;
let duration = from_secs_f64;
Trait Implementations
Compare two values for equality.
Examples
assert_eq!;
assert_eq!;
assert_eq!;
Compare two values for inequality.
Examples
assert_eq!;
assert_eq!;
assert_eq!;
Compare two values.
Examples
use Ordering;
assert_eq!;
assert_eq!;
assert_eq!;
Tests less than (for self
and other
) and is used by the <
operator.
Examples
assert_eq!;
assert_eq!;
assert_eq!;
Tests less than or equal to (for self
and other
) and is used by the <=
operator.
Examples
assert_eq!;
assert_eq!;
assert_eq!;
Tests greater than (for self
and other
) and is used by the >
operator.
Examples
assert_eq!;
assert_eq!;
assert_eq!;
Tests greater than or equal to (for self
and other
) and is used by the >=
operator.
Examples
assert_eq!;
assert_eq!;
assert_eq!;
Compare two values.
Examples
use Ordering;
assert_eq!;
assert_eq!;
assert_eq!;
Return the minimum of two values.
Examples
assert_eq!;
assert_eq!;
assert_eq!;
Return the maximum of two values.
Examples
assert_eq!;
assert_eq!;
assert_eq!;
Clone the specified value
.
Examples
let a = 42;
let b = a;
let c = a.clone;
a += 1;
assert_eq!;
assert_eq!;
assert_eq!;
Protocols
let $out = value + $b
Add a duration to this instant and return a new instant.
Examples
use Duration;
let first = SECOND;
let second = first + SECOND;
assert!;
value += $b
Add a duration to this instant and return a new instant.
Examples
use Duration;
let first = SECOND;
let second = first.clone;
second += SECOND;
assert!;
if value == b
Test two durations for partial equality.
Examples
use partial_eq;
use Duration;
let millis = MILLISECOND;
let second = SECOND;
assert_eq!;
assert_eq!;
assert_eq!;
if value == b
Test two durations for total equality.
Examples
use eq;
use Duration;
let millis = MILLISECOND;
let second = SECOND;
assert_eq!;
assert_eq!;
assert_eq!;
if value < b
Perform a partial ordered comparison between two durations.
Examples
use Duration;
let millis = MILLISECOND;
let second = SECOND;
assert!;
assert!;
assert!;
Using explicit functions:
use Ordering;
use partial_cmp;
use Duration;
let millis = MILLISECOND;
let second = SECOND;
assert_eq!;
assert_eq!;
assert_eq!;
if value < b
Perform a totally ordered comparison between two durations.
Examples
use Ordering;
use cmp;
use Duration;
let millis = MILLISECOND;
let second = SECOND;
assert_eq!;
assert_eq!;
assert_eq!;
let $out = hash
Hash the duration.
Examples
use hash;
use Duration;
let second = SECOND;
assert_eq!;
format!
Write a debug representation of the duration.
Examples
use Duration;
let second = SECOND;
println!;
let $out = clone
Clone the current duration.
Examples
use Duration;
let first = SECOND;
let second = SECOND;
second += SECOND;
assert!;