Struct time::Instant

Overview

A measurement of a monotonically nondecreasing clock. Opaque and useful only with Duration.

Instants are always guaranteed to be no less than any previously measured instant when created, and are often useful for tasks such as measuring benchmarks or timing how long an operation takes.

Note, however, that instants are not guaranteed to be steady. In other words, each tick of the underlying clock may not be the same length (e.g. some seconds may be longer than others). An instant may jump forwards or experience time dilation (slow down or speed up), but it will never go backwards.

Instants are opaque types that can only be compared to one another. There is no method to get "the number of seconds" from an instant. Instead, it only allows measuring the duration between two instants (or comparing two instants).

The size of an Instant struct may vary depending on the target operating system.

Methods

fn now() -> Instant

Returns an instant corresponding to now.

Examples

use time::{Duration, Instant};

let instant = Instant::now();
fn duration_since(self, earlier: Instant) -> Duration

Returns the amount of time elapsed from another instant to this one, or zero duration if that instant is later than this one.

Examples

use time::{Duration, Instant};

let instant = Instant::now();

let three_secs = Duration::from_secs(3);
time::sleep(three_secs).await;

let now = Instant::now();
let duration_since = now.duration_since(instant);

Returns the amount of time elapsed since this instant was created, or zero duration if that this instant is in the future.

Examples

use time::{Duration, Instant};

let instant = Instant::now();

let three_secs = Duration::from_secs(3);
time::sleep(three_secs).await;

let elapsed = instant.elapsed();

Trait Implementations

impl PartialEq for Instant
fn eq(value: any, value1: any) -> bool

Compare two values for equality.

Examples

assert_eq!(1.eq(2), false);
assert_eq!(2.eq(2), true);
assert_eq!(2.eq(1), false);
fn ne(value: any, value1: any) -> bool

Compare two values for inequality.

Examples

assert_eq!(1.ne(2), true);
assert_eq!(2.ne(2), false);
assert_eq!(2.ne(1), true);
impl Eq for Instant
impl PartialOrd for Instant
fn partial_cmp(value: any, value1: any) -> Option

Compare two values.

Examples

use std::cmp::Ordering;

assert_eq!(1.partial_cmp(2), Some(Ordering::Less));
assert_eq!(2.partial_cmp(2), Some(Ordering::Equal));
assert_eq!(2.partial_cmp(1), Some(Ordering::Greater));
fn lt(value: any, value1: any) -> bool

Tests less than (for self and other) and is used by the < operator.

Examples

assert_eq!(1.0 < 1.0, false);
assert_eq!(1.0 < 2.0, true);
assert_eq!(2.0 < 1.0, false);
fn le(value: any, value1: any) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator.

Examples

assert_eq!(1.0 <= 1.0, true);
assert_eq!(1.0 <= 2.0, true);
assert_eq!(2.0 <= 1.0, false);
fn gt(value: any, value1: any) -> bool

Tests greater than (for self and other) and is used by the > operator.

Examples

assert_eq!(1.0 > 1.0, false);
assert_eq!(1.0 > 2.0, false);
assert_eq!(2.0 > 1.0, true);
fn ge(value: any, value1: any) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator.

Examples

assert_eq!(1.0 >= 1.0, true);
assert_eq!(1.0 >= 2.0, false);
assert_eq!(2.0 >= 1.0, true);
impl Ord for Instant
fn cmp(value: any, value1: any) -> Ordering

Compare two values.

Examples

use std::cmp::Ordering;

assert_eq!(1.cmp(2), Ordering::Less);
assert_eq!(2.cmp(2), Ordering::Equal);
assert_eq!(2.cmp(1), Ordering::Greater);
fn min(value: any, value1: any) -> Ordering

Return the minimum of two values.

Examples

assert_eq!(1.min(2), 1);
assert_eq!(2.min(2), 2);
assert_eq!(2.min(1), 1);
fn max(value: any, value1: any) -> Ordering

Return the maximum of two values.

Examples

assert_eq!(1.max(2), 2);
assert_eq!(2.max(2), 2);
assert_eq!(2.max(1), 2);
impl Clone for Instant
fn clone(value: any) -> any

Clone the specified value.

Examples

let a = 42;
let b = a;
let c = a.clone();

a += 1;

assert_eq!(a, 43);
assert_eq!(b, 42);
assert_eq!(c, 42);

Protocols

protocol ADD
let $out = value + $b

Add a duration to this instant and return a new instant.

Examples

use time::{Duration, Instant};

let first = Instant::now();
let second = first + Duration::SECOND;

assert!(first < second);
protocol ADD_ASSIGN
value += $b

Add a duration to this instant and return a new instant.

Examples

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

let first = Instant::now();
let second = first.clone();
second += Duration::SECOND;

assert!(first < second);
protocol PARTIAL_EQ
if value == b { }

Test two instants for partial equality.

Examples

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

let first = Instant::now();
let second = first + Duration::SECOND;

assert_eq!(partial_eq(first, first), true);
assert_eq!(partial_eq(first, second), false);
assert_eq!(partial_eq(second, first), false);
protocol EQ
if value == b { }

Test two instants for total equality.

Examples

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

let first = Instant::now();
let second = first + Duration::SECOND;

assert_eq!(eq(first, first), true);
assert_eq!(eq(first, second), false);
assert_eq!(eq(second, first), false);
protocol PARTIAL_CMP
if value < b { }

Perform a partial ordered comparison between two instants.

Examples

use time::{Duration, Instant};

let first = Instant::now();
let second = first + Duration::SECOND;

assert!(first < second);
assert!(second > first);
assert!(first == first);

Using explicit functions:

use std::cmp::Ordering;
use std::ops::partial_cmp;

use time::{Duration, Instant};

let first = Instant::now();
let second = first + Duration::SECOND;

assert_eq!(partial_cmp(first, second), Some(Ordering::Less));
assert_eq!(partial_cmp(second, first), Some(Ordering::Greater));
assert_eq!(partial_cmp(first, first), Some(Ordering::Equal));
protocol CMP
if value < b { }

Perform a totally ordered comparison between two instants.

Examples

use std::cmp::Ordering;
use std::ops::cmp;
use time::{Duration, Instant};

let first = Instant::now();
let second = first + Duration::SECOND;

assert_eq!(cmp(first, second), Ordering::Less);
assert_eq!(cmp(second, first), Ordering::Greater);
assert_eq!(cmp(first, first), Ordering::Equal);
protocol HASH
let $out = hash(value)

Hash the instant.

Examples

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

let now = Instant::now();

assert_eq!(hash(now), hash(now));
protocol DEBUG_FMT
format!("{:?}", value)

Write a debug representation of the instant.

Examples

use time::Instant;

let now = Instant::now();

println!("{now:?}");
protocol CLONE
let $out = clone(value)

Clone the current instant.

Examples

use time::{Duration, Instant};

let first = Instant::now();
let second = first.clone();
second += Duration::SECOND;

assert!(first < second);
protocol LT
if $a < $b { }

The protocol behind the < operator.

protocol LE
if $a <= $b { }

The protocol behind the <= operator.

protocol GT
if $a > $b { }

The protocol behind the > operator.

protocol GE
if $a >= $b { }

The protocol behind the >= operator.

protocol MIN
$a.min($b)

The implementation protocol for the PartialOrd::min method.

protocol MAX
$a.max($b)

The implementation protocol for the PartialOrd::max method.