# Struct std::time::Duration [−] [src]

    pub struct Duration {
        // some fields omitted
    }

A duration type to represent a span of time, typically used for system timeouts.

Each duration is composed of a number of seconds and nanosecond precision. APIs binding a system timeout will typically round up the nanosecond precision if the underlying system does not support that level of precision.

Durations implement many common traits, including `Add`, `Sub`, and other ops traits. Currently a duration may only be inspected for its number of seconds and its nanosecond precision.

# Examples

fn main() { use std::time::Duration; let five_seconds = Duration::new(5, 0); let five_seconds_and_five_nanos = five_seconds + Duration::new(0, 5); assert_eq!(five_seconds_and_five_nanos.as_secs(), 5); assert_eq!(five_seconds_and_five_nanos.subsec_nanos(), 5); let ten_millis = Duration::from_millis(10); }

    use std::time::Duration;

    let five_seconds = Duration::new(5, 0);
    let five_seconds_and_five_nanos = five_seconds + Duration::new(0, 5);

    assert_eq!(five_seconds_and_five_nanos.as_secs(), 5);
    assert_eq!(five_seconds_and_five_nanos.subsec_nanos(), 5);

    let ten_millis = Duration::from_millis(10);

## Methods

### `impl Duration`

#### `fn new(secs: u64, nanos: u32) -> Duration`

Creates a new `Duration` from the specified number of seconds and additional nanosecond precision.

If the nanoseconds is greater than 1 billion (the number of nanoseconds in a second), then it will carry over into the seconds provided.

#### `fn span<F>(f: F) -> Duration where F: FnOnce()`

Deprecated since 1.6.0

: use std::time::Instant instead

Runs a closure, returning the duration of time it took to run the closure.

#### `fn from_secs(secs: u64) -> Duration`

Creates a new `Duration` from the specified number of seconds.

#### `fn from_millis(millis: u64) -> Duration`

Creates a new `Duration` from the specified number of milliseconds.

#### `fn as_secs(&self) -> u64`

Returns the number of whole seconds represented by this duration.

The extra precision represented by this duration is ignored (e.g. extra nanoseconds are not represented in the returned value).

#### `fn subsec_nanos(&self) -> u32`

Returns the nanosecond precision represented by this duration.

This method does not return the length of the duration when represented by nanoseconds. The returned number always represents a fractional portion of a second (e.g. it is less than one billion).

## Trait Implementations

### `impl Add for Duration`

#### `type Output = Duration`

#### `fn add(self, rhs: Duration) -> Duration`

### `impl Sub for Duration`

#### `type Output = Duration`

#### `fn sub(self, rhs: Duration) -> Duration`

### `impl Mul<u32> for Duration`

#### `type Output = Duration`

#### `fn mul(self, rhs: u32) -> Duration`

### `impl Div<u32> for Duration`

#### `type Output = Duration`

#### `fn div(self, rhs: u32) -> Duration`

### Derived Implementations

### `impl Hash for Duration`

#### `fn hash<__H: Hasher>(&self, __arg_0: &mut __H)`

#### `fn hash_slice<H>(data: &[Self], state: &mut H) where H: Hasher`

### `impl Debug for Duration`

#### `fn fmt(&self, __arg_0: &mut Formatter) -> Result`

### `impl Ord for Duration`

#### `fn cmp(&self, __arg_0: &Duration) -> Ordering`

### `impl PartialOrd for Duration`

#### `fn partial_cmp(&self, __arg_0: &Duration) -> Option<Ordering>`

#### `fn lt(&self, __arg_0: &Duration) -> bool`

#### `fn le(&self, __arg_0: &Duration) -> bool`

#### `fn gt(&self, __arg_0: &Duration) -> bool`

#### `fn ge(&self, __arg_0: &Duration) -> bool`

### `impl Eq for Duration`

### `impl PartialEq for Duration`

#### `fn eq(&self, __arg_0: &Duration) -> bool`

#### `fn ne(&self, __arg_0: &Duration) -> bool`

### `impl Copy for Duration`

### `impl Clone for Duration`

#### `fn clone(&self) -> Duration`

#### `fn clone_from(&mut self, source: &Self)`

# Help

## Keyboard Shortcuts

?
  Show this help dialog
S
  Focus the search field
⇤
  Move up in search results
⇥
  Move down in search results
⏎
  Go to active search result

## Search Tricks

Prefix searches with a type followed by a colon (e.g. `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g. `vec -> usize`)