Trait time::ext::NumericalStdDurationShort
source · [−]pub trait NumericalStdDurationShort {
fn nanoseconds(self) -> StdDuration;
fn microseconds(self) -> StdDuration;
fn milliseconds(self) -> StdDuration;
fn seconds(self) -> StdDuration;
fn minutes(self) -> StdDuration;
fn hours(self) -> StdDuration;
fn days(self) -> StdDuration;
fn weeks(self) -> StdDuration;
}
Expand description
Create std::time::Duration
s from primitive and core numeric types. Unless
you are always expecting a std::time::Duration
, you should prefer to use
NumericalStdDuration
for clarity.
Due to limitations in rustc, these methods are currently not const fn
.
See this RFC for details.
Examples
Basic construction of std::time::Duration
s.
assert_eq!(5.nanoseconds(), Duration::from_nanos(5));
assert_eq!(5.microseconds(), Duration::from_micros(5));
assert_eq!(5.milliseconds(), Duration::from_millis(5));
assert_eq!(5.seconds(), Duration::from_secs(5));
assert_eq!(5.minutes(), Duration::from_secs(5 * 60));
assert_eq!(5.hours(), Duration::from_secs(5 * 3_600));
assert_eq!(5.days(), Duration::from_secs(5 * 86_400));
assert_eq!(5.weeks(), Duration::from_secs(5 * 604_800));
Just like any other std::time::Duration
, they can be added, subtracted,
etc.
assert_eq!(2.seconds() + 500.milliseconds(), 2_500.milliseconds());
assert_eq!(2.seconds() - 500.milliseconds(), 1_500.milliseconds());
When called on floating point values, any remainder of the floating point value will be truncated. Keep in mind that floating point numbers are inherently imprecise and have limited capacity.
Required methods
fn nanoseconds(self) -> StdDuration
fn nanoseconds(self) -> StdDuration
Create a std::time::Duration
from the number of nanoseconds.
fn microseconds(self) -> StdDuration
fn microseconds(self) -> StdDuration
Create a std::time::Duration
from the number of microseconds.
fn milliseconds(self) -> StdDuration
fn milliseconds(self) -> StdDuration
Create a std::time::Duration
from the number of milliseconds.
fn seconds(self) -> StdDuration
fn seconds(self) -> StdDuration
Create a std::time::Duration
from the number of seconds.
fn minutes(self) -> StdDuration
fn minutes(self) -> StdDuration
Create a std::time::Duration
from the number of minutes.
fn hours(self) -> StdDuration
fn hours(self) -> StdDuration
Create a std::time::Duration
from the number of hours.
fn days(self) -> StdDuration
fn days(self) -> StdDuration
Create a std::time::Duration
from the number of days.
fn weeks(self) -> StdDuration
fn weeks(self) -> StdDuration
Create a std::time::Duration
from the number of weeks.