Struct std::time::Instant1.8.0[][src]

pub struct Instant(_);
Expand description

单调非递减时钟的度量。 不透明且仅对 Duration 有用。

创建时始终保证瞬时不小于任何先前测量的瞬时,并且对于执行诸如测量基准或计时操作花费多长时间等任务通常很有用。

但是请注意,不能保证瞬间稳定。换句话说,底层时钟的每个滴答声的长度可能不同 (例如 几秒钟可能比其他更长)。瞬间可能会向前跳跃或经历时间膨胀 (减速或加速),但永远不会向后退。

即时消息是不透明的类型,只能相互比较。没有方法可以立即获取 “秒数”。 相反,它仅允许测量两个瞬间之间的持续时间 (或比较两个瞬间)。

Instant 结构体的大小可能会因目标操作系统而异。

Example:

use std::time::{Duration, Instant};
use std::thread::sleep;

fn main() {
   let now = Instant::now();

   // 我们睡了 2 秒钟
   sleep(Duration::new(2, 0));
   // 它打印 '2'
   println!("{}", now.elapsed().as_secs());
}
Run

特定于操作系统的行为

Instant 是系统特定类型的包装器,根据底层操作系统的不同,它的行为可能会有所不同。 例如,以下代码段在 Linux 上很好,但在 macOS 上为 panics:

use std::time::{Instant, Duration};

let now = Instant::now();
let max_nanoseconds = u64::MAX / 1_000_000_000;
let duration = Duration::new(max_nanoseconds, 0);
println!("{:?}", now + duration);
Run

底层系统调用

当前,正在使用以下系统调用来使用 now() 获取当前时间:

PlatformSystem call
SGXinsecure_time usercall. More information on timekeeping in SGX
UNIXclock_gettime (Monotonic Clock)
Darwinmach_absolute_time
VXWorksclock_gettime (Monotonic Clock)
SOLIDget_tim
WASI__wasi_clock_time_get (Monotonic Clock)
WindowsQueryPerformanceCounter

免责声明: 这些系统调用可能会随时间变化。

Note: 如果 add 的数学运算可能是 panic 结构体不能代表新的时间点。

Implementations

返回对应于 “now” 的瞬间。

Examples
use std::time::Instant;

let now = Instant::now();
Run

返回从另一个时刻到该时刻所经过的时间。

Panics

如果 earlier 晚于 self,则此函数将为 panic。

Examples
use std::time::{Duration, Instant};
use std::thread::sleep;

let now = Instant::now();
sleep(Duration::new(1, 0));
let new_now = Instant::now();
println!("{:?}", new_now.duration_since(now));
Run

返回从另一个时刻到该时刻所经过的时间; 如果该时刻晚于该时刻,则返回 None。

Examples
use std::time::{Duration, Instant};
use std::thread::sleep;

let now = Instant::now();
sleep(Duration::new(1, 0));
let new_now = Instant::now();
println!("{:?}", new_now.checked_duration_since(now));
println!("{:?}", now.checked_duration_since(new_now)); // None
Run

返回从另一时刻到该时刻所经过的时间,如果该时刻晚于该时刻,则返回零持续时间。

Examples
use std::time::{Duration, Instant};
use std::thread::sleep;

let now = Instant::now();
sleep(Duration::new(1, 0));
let new_now = Instant::now();
println!("{:?}", new_now.saturating_duration_since(now));
println!("{:?}", now.saturating_duration_since(new_now)); // 0ns
Run

返回自创建此瞬间以来经过的时间。

Panics

如果当前时间早于此时刻,则此函数可能为 panic,如果 Instant 是综合生成的,则可能会发生这种情况。

Examples
use std::thread::sleep;
use std::time::{Duration, Instant};

let instant = Instant::now();
let three_secs = Duration::from_secs(3);
sleep(three_secs);
assert!(instant.elapsed() >= three_secs);
Run

如果 t 可以表示为 Instant (这意味着它在底层数据结构的边界内),则返回 Some(t),其中 t 是时间 self + duration,否则返回 None

如果 t 可以表示为 Instant (表示它在底层数据结构体的边界之内),则返回 Some(t),其中 tself - duration 的时间,否则返回 None

Trait Implementations

Panics

如果生成的时间点无法由底层数据结构表示,则此函数可能出现 panic。 没有 panic 的版本,请参见 Instant::checked_add

应用 + 运算符后的结果类型。

执行 += 操作。 Read more

返回值的副本。 Read more

source 执行复制分配。 Read more

使用给定的格式化程序格式化该值。 Read more

将该值输入给定的 HasherRead more

将这种类型的切片送入给定的 Hasher 中。 Read more

此方法返回 selfother 之间的 OrderingRead more

比较并返回两个值中的最大值。 Read more

比较并返回两个值中的最小值。 Read more

将值限制在某个时间间隔内。 Read more

此方法测试 selfother 值是否相等,并由 == 使用。 Read more

此方法测试 !=

如果存在,则此方法返回 selfother 值之间的顺序。 Read more

此方法测试的内容少于 (对于 selfother),并且由 < 操作员使用。 Read more

此方法测试小于或等于 (对于 selfother),并且由 <= 运算符使用。 Read more

此方法测试大于 (对于 selfother),并且由 > 操作员使用。 Read more

此方法测试是否大于或等于 (对于 selfother),并且由 >= 运算符使用。 Read more

应用 - 运算符后的结果类型。

执行 - 操作。 Read more

应用 - 运算符后的结果类型。

执行 - 操作。 Read more

执行 -= 操作。 Read more

Auto Trait Implementations

Blanket Implementations

获取 selfTypeIdRead more

从拥有的值中一成不变地借用。 Read more

从拥有的值中借用。 Read more

执行转换。

执行转换。

获得所有权后的结果类型。

从借用的数据创建拥有的数据,通常是通过克隆。 Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into #41263)

使用借来的数据来替换拥有的数据,通常是通过克隆。 Read more

发生转换错误时返回的类型。

执行转换。

发生转换错误时返回的类型。

执行转换。