logo
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::Durations 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::Durations.

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

Create a std::time::Duration from the number of nanoseconds.

Create a std::time::Duration from the number of microseconds.

Create a std::time::Duration from the number of milliseconds.

Create a std::time::Duration from the number of seconds.

Create a std::time::Duration from the number of minutes.

Create a std::time::Duration from the number of hours.

Create a std::time::Duration from the number of days.

Create a std::time::Duration from the number of weeks.

Implementors