duration
import std::time::duration; · source
Types
Duration
type struct Duration {
secs: u64;
nanos: u32;
static new(secs: u64, nanos: u32) -> Duration;
static zero() -> Duration;
static second() -> Duration;
static millisecond() -> Duration;
static microsecond() -> Duration;
static nanosecond() -> Duration;
static from_secs(secs: u64) -> Duration;
static from_millis(millis: u64) -> Duration;
static from_micros(micros: u64) -> Duration;
static from_nanos(nanos: u64) -> Duration;
static from_secs_f64(secs: f64) -> Duration;
as_secs(&this) -> u64;
subsec_nanos(&this) -> u32;
subsec_millis(&this) -> u32;
subsec_micros(&this) -> u32;
as_millis(&this) -> u64;
as_micros(&this) -> u64;
as_nanos(&this) -> u64;
as_secs_f64(&this) -> f64;
is_zero(&this) -> boolean;
checked_add(&this, other: &Duration) -> Option<Duration>;
checked_sub(&this, other: &Duration) -> Option<Duration>;
saturating_add(&this, other: &Duration) -> Duration;
saturating_sub(&this, other: &Duration) -> Duration;
scale(&this, factor: u64) -> Duration;
mul_f64(&this, factor: f64) -> Duration;
divide(&this, divisor: u64) -> Duration;
to_string(&this) -> String;
to_debug_string(&this) -> String;
}
Whole seconds plus a sub-second nanosecond remainder, always normalized so that nanos < 1_000_000_000.
import std::time;
const timeout: Duration = Duration::from_millis(250);
sleep(timeout);
Properties
| Property | Type |
|---|---|
secs | u64 |
nanos | u32 |
Constructors
static new(secs: u64, nanos: u32) -> Duration;
static new(secs: u64, nanos: u32) -> Duration;Construct from a (seconds, nanoseconds) pair, carrying any nanosecond overflow (>= 1e9) into the seconds field so the stored value is always normalized.
static zero() -> Duration;
static zero() -> Duration;The zero duration.
static second() -> Duration;
static second() -> Duration;One second.
static millisecond() -> Duration;
static millisecond() -> Duration;One millisecond.
static microsecond() -> Duration;
static microsecond() -> Duration;One microsecond.
static nanosecond() -> Duration;
static nanosecond() -> Duration;One nanosecond.
static from_secs(secs: u64) -> Duration;
static from_millis(millis: u64) -> Duration;
static from_micros(micros: u64) -> Duration;
static from_nanos(nanos: u64) -> Duration;
static from_secs_f64(secs: f64) -> Duration;
static from_secs_f64(secs: f64) -> Duration;Construct from a floating-point number of seconds. Negative inputs clamp to zero.
Methods
as_secs(&this) -> u64;
as_secs(&this) -> u64;Whole seconds (the sub-second part is dropped).
subsec_nanos(&this) -> u32;
subsec_nanos(&this) -> u32;The sub-second remainder in nanoseconds (< 1e9).
subsec_millis(&this) -> u32;
subsec_millis(&this) -> u32;The sub-second remainder in whole milliseconds (< 1000).
subsec_micros(&this) -> u32;
subsec_micros(&this) -> u32;The sub-second remainder in whole microseconds (< 1_000_000).
as_millis(&this) -> u64;
as_millis(&this) -> u64;Total whole milliseconds.
as_micros(&this) -> u64;
as_micros(&this) -> u64;Total whole microseconds.
as_nanos(&this) -> u64;
as_nanos(&this) -> u64;Total nanoseconds. Wraps for spans beyond ~584 years.
as_secs_f64(&this) -> f64;
as_secs_f64(&this) -> f64;The whole span as floating-point seconds.
is_zero(&this) -> boolean;
checked_add(&this, other: &Duration) -> Option<Duration>;
checked_add(&this, other: &Duration) -> Option<Duration>;this + other, or None if the seconds field would overflow.
checked_sub(&this, other: &Duration) -> Option<Duration>;
checked_sub(&this, other: &Duration) -> Option<Duration>;this - other, or None when other is the larger.
saturating_add(&this, other: &Duration) -> Duration;
saturating_add(&this, other: &Duration) -> Duration;this + other, saturating at the maximum representable span on overflow.
saturating_sub(&this, other: &Duration) -> Duration;
saturating_sub(&this, other: &Duration) -> Duration;this - other, saturating at zero when other is the larger.
scale(&this, factor: u64) -> Duration;
scale(&this, factor: u64) -> Duration;this * factor.
mul_f64(&this, factor: f64) -> Duration;
mul_f64(&this, factor: f64) -> Duration;this * factor for a floating-point factor.
divide(&this, divisor: u64) -> Duration;
divide(&this, divisor: u64) -> Duration;this / divisor. Panics on division by zero.
to_string(&this) -> String;
to_string(&this) -> String;Render the most natural unit as an owned String (1.5s, 250ms, 10ns) - the same text as the Display impl, available directly without an f-string.
to_debug_string(&this) -> String;
to_debug_string(&this) -> String;Render the Debug form (Duration { secs: S, nanos: N }) as an owned String.
Building
new(secs, nanos) carries any nanosecond overflow into the seconds field, so the stored value is always normalized. Then the unit constructors — from_secs, from_millis, from_micros, from_nanos, and from_secs_f64 (negative input clamps to zero) — plus the named constants zero(), second(), millisecond(), microsecond(), and nanosecond().
Reading
as_secs() drops the sub-second part; as_millis(), as_micros(), and as_nanos() give the whole span in that unit and as_secs_f64() as fractional seconds; the subsec_* accessors give just the remainder.
Arithmetic
checked_add and checked_sub return Option<Duration>, None on overflow or underflow. saturating_add and saturating_sub clamp instead. scale(factor), mul_f64(factor), and divide(divisor) scale the span; mul_f64 clamps a negative factor to zero.
to_string() renders in the largest sensible unit — 1.5s, 250ms, 10ns — the same text as the Display impl, and to_debug_string() the Debug form.
Trait implementations
implement trait Eq for struct Duration
implement trait Ord for struct Duration
implement trait Display for struct Duration
implement trait Debug for struct Duration
Constants
const NANOS_PER_SEC: u64 = 1000000000;
const NANOS_PER_MILLI: u64 = 1000000;
const NANOS_PER_MICRO: u64 = 1000;
const MILLIS_PER_SEC: u64 = 1000;
const MICROS_PER_SEC: u64 = 1000000;
const U64_MAX: u64 = 0xFFFFFFFFFFFFFFFF;type struct Name { ... }
type enum Name { ... }
type trait Name { ... }
type union Name { ... }Begins a type declaration; the keyword that follows says which kind. On its own, type Name = Existing; declares an alias.
type struct Name {
field: Type;
method(&this) -> Ret { ... }
}A value type with named fields and optional methods. Structs live on the stack and are passed by value. Fields are public by default and may carry = default values; methods take their receiver as &this, mut &this, or this.
type struct Duration {
secs: u64;
nanos: u32;
}A length of time: whole secs plus a sub-second nanos remainder (0 <= nanos < 1_000_000_000). POD; copy-by-value, no drop.
Duration.secs: u64Duration.nanos: u32static name(param: Type) -> Ret { ... }A method that belongs to the type rather than an instance, called as Type::name(..). For structs, static new(..) returning a struct literal is the idiomatic constructor.
static Duration::new(secs: u64, nanos: u32) -> DurationConstruct from a (seconds, nanoseconds) pair, carrying any nanosecond overflow (>= 1e9) into the seconds field so the stored value is always normalized.
(parameter) secs: u64(parameter) nanos: u32type struct Duration {
secs: u64;
nanos: u32;
}A length of time: whole secs plus a sub-second nanos remainder (0 <= nanos < 1_000_000_000). POD; copy-by-value, no drop.
static Duration::zero() -> DurationThe zero duration.
static Duration::second() -> DurationOne second.
static Duration::millisecond() -> DurationOne millisecond.
static Duration::microsecond() -> DurationOne microsecond.
static Duration::nanosecond() -> DurationOne nanosecond.
static Duration::from_secs(secs: u64) -> Durationstatic Duration::from_millis(millis: u64) -> Duration(parameter) millis: u64static Duration::from_micros(micros: u64) -> Duration(parameter) micros: u64static Duration::from_nanos(nanos: u64) -> Duration(parameter) nanos: u64static Duration::from_secs_f64(secs: f64) -> DurationConstruct from a floating-point number of seconds. Negative inputs clamp to zero.
(parameter) secs: f64Duration::as_secs(&this) -> u64Whole seconds (the sub-second part is dropped).
&this
mut &this
thisThe receiver of a method. Its first parameter declares how the method takes it: &this borrows read-only, mut &this borrows exclusively, and this / mut this move the value in and consume it. In the body, this.field reaches the members.
Duration::subsec_nanos(&this) -> u32The sub-second remainder in nanoseconds (< 1e9).
Duration::subsec_millis(&this) -> u32The sub-second remainder in whole milliseconds (< 1000).
Duration::subsec_micros(&this) -> u32The sub-second remainder in whole microseconds (< 1_000_000).
Duration::as_millis(&this) -> u64Total whole milliseconds.
Duration::as_micros(&this) -> u64Total whole microseconds.
Duration::as_nanos(&this) -> u64Total nanoseconds. Wraps for spans beyond ~584 years.
Duration::as_secs_f64(&this) -> f64The whole span as floating-point seconds.
Duration::is_zero(&this) -> booleanDuration::checked_add(&this, other: &Duration) -> Option<Duration>this + other, or None if the seconds field would overflow.
(parameter) other: &DurationDuration::checked_sub(&this, other: &Duration) -> Option<Duration>this - other, or None when other is the larger.
Duration::saturating_add(&this, other: &Duration) -> Durationthis + other, saturating at the maximum representable span on overflow.
Duration::saturating_sub(&this, other: &Duration) -> Durationthis - other, saturating at zero when other is the larger.
Duration::scale(&this, factor: u64) -> Durationthis * factor.
(parameter) factor: u64Duration::mul_f64(&this, factor: f64) -> Durationthis * factor for a floating-point factor.
(parameter) factor: f64Duration::divide(&this, divisor: u64) -> Durationthis / divisor. Panics on division by zero.
(parameter) divisor: u64Duration::to_string(&this) -> StringRender the most natural unit as an owned String (1.5s, 250ms, 10ns) - the same text as the Display impl, available directly without an f-string.
type struct String<A = GlobalAlloc> {
buffer: RawBuffer<u8, A>;
length: u64;
}String: owned, heap-allocated, length-typed UTF-8.
std::collections::stringDuration::to_debug_string(&this) -> StringRender the Debug form (Duration { secs: S, nanos: N }) as an owned String.
import a::b;
import a::b::{ X, Y };
import a::b as c;
import a::b::*;Brings a module, or selected items from it, into scope. One path per declaration: use the brace form for several items, as to alias, and * for everything public (prefer the brace form; wildcards invite name collisions).
const NAME: Type = value;At module level, a true compile-time constant, conventionally SCREAMING_SNAKE_CASE. (mut at module level declares mutable global state instead.)
const timeout: Durationstatic Duration::from_millis(millis: u64) -> Durationfunction sleep(dur: Duration) -> voidPark the current thread for at least dur. POSIX path restarts across EINTR using the kernel's remaining-time readout; Windows Sleep is uninterruptible at the millisecond granularity it accepts. Sub-millisecond dur rounds UP to 1 ms on Windows so a non-zero request always blocks.
const timeout: Durationconst NANOS_PER_SEC: u64 = 1000000000;const NANOS_PER_MILLI: u64 = 1000000;const NANOS_PER_MICRO: u64 = 1000;const MILLIS_PER_SEC: u64 = 1000;const MICROS_PER_SEC: u64 = 1000000;const U64_MAX: u64 = 0xFFFFFFFFFFFFFFFF;Duration.nanos: u32Duration.secs: u64static Duration::new(secs: u64, nanos: u32) -> DurationConstruct from a (seconds, nanoseconds) pair, carrying any nanosecond overflow (>= 1e9) into the seconds field so the stored value is always normalized.
static Duration::from_secs(secs: u64) -> Durationstatic Duration::from_micros(micros: u64) -> Durationstatic Duration::from_nanos(nanos: u64) -> Durationstatic Duration::from_secs_f64(secs: f64) -> DurationConstruct from a floating-point number of seconds. Negative inputs clamp to zero.
std::time::durationDuration::subsec_nanos(&this) -> u32The sub-second remainder in nanoseconds (< 1e9).
Duration::subsec_millis(&this) -> u32The sub-second remainder in whole milliseconds (< 1000).
Duration::subsec_micros(&this) -> u32The sub-second remainder in whole microseconds (< 1_000_000).
Duration::as_nanos(&this) -> u64Total nanoseconds. Wraps for spans beyond ~584 years.
std::time::durationDuration::is_zero(&this) -> booleanDuration::checked_add(&this, other: &Duration) -> Option<Duration>this + other, or None if the seconds field would overflow.
Option::NoneDuration::checked_sub(&this, other: &Duration) -> Option<Duration>this - other, or None when other is the larger.
Duration::saturating_add(&this, other: &Duration) -> Durationthis + other, saturating at the maximum representable span on overflow.
Duration::saturating_sub(&this, other: &Duration) -> Durationthis - other, saturating at zero when other is the larger.
Duration::mul_f64(&this, factor: f64) -> Durationthis * factor for a floating-point factor.
Duration::divide(&this, divisor: u64) -> Durationthis / divisor. Panics on division by zero.
Duration::to_string(&this) -> StringRender the most natural unit as an owned String (1.5s, 250ms, 10ns) - the same text as the Display impl, available directly without an f-string.
type trait Display {
fmt<W>(&this, f: mut &Formatter<W>) -> Result<(), FmtError>
where W: FmtWrite;
}Display / Debug / Formatter.
std::fmt::displayDuration::to_debug_string(&this) -> StringRender the Debug form (Duration { secs: S, nanos: N }) as an owned String.
type trait Debug {
fmt<W>(&this, f: mut &Formatter<W>) -> Result<(), FmtError>
where W: FmtWrite;
}implement struct Name { ... }
implement trait Trait for Type { ... }
implement u64 { ... }Adds methods to a type outside its declaration. implement struct T (or enum, or a bare primitive) opens an inherent block; implement trait Tr for T supplies a trait's methods for one type. Resolution is at compile time - no dynamic dispatch.
implement trait Trait for Type { ... }Introduces a trait implementation: the block supplies, for one concrete type, the method bodies the trait requires.
language referencetype trait Eq {
equals(&this, other: &This) -> boolean;
}Types that support value equality. equals(a, b) must be reflexive, symmetric, and transitive.
implement trait Trait for Type { ... }Names the type a trait implementation is for. The block that follows supplies the bodies the trait requires.
language referencetype trait Ord : Eq {
compare(&this, other: &This) -> Ordering;
}Types that have a total order: every pair is Less, Equal, or Greater. compare must be consistent with equals: if a.equals(b) then a.compare(b) == Equal.