Skip to content

Commit faf5985

Browse files
committed
Adding examples to docs of std::time module
And adding missing link to Duration from Instant
1 parent daa4dc9 commit faf5985

File tree

2 files changed

+37
-11
lines changed

2 files changed

+37
-11
lines changed

library/core/src/time.rs

+12-5
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,21 @@
22

33
//! Temporal quantification.
44
//!
5-
//! Example:
5+
//! # Examples:
6+
//!
7+
//! There are multiple ways to create a new [`Duration`]:
68
//!
79
//! ```
8-
//! use std::time::Duration;
10+
//! # use std::time::Duration;
11+
//! let five_seconds = Duration::from_secs(5);
12+
//! assert_eq!(five_seconds, Duration::from_millis(5_000));
13+
//! assert_eq!(five_seconds, Duration::from_micros(5_000_000));
14+
//! assert_eq!(five_seconds, Duration::from_nanos(5_000_000_000));
915
//!
10-
//! let five_seconds = Duration::new(5, 0);
11-
//! // both declarations are equivalent
12-
//! assert_eq!(Duration::new(5, 0), Duration::from_secs(5));
16+
//! let ten_seconds = Duration::from_secs(10);
17+
//! let seven_nanos = Duration::from_nanos(7);
18+
//! let total = ten_seconds + seven_nanos;
19+
//! assert_eq!(total, Duration::new(10, 7));
1320
//! ```
1421
1522
use crate::fmt;

library/std/src/time.rs

+25-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,32 @@
11
//! Temporal quantification.
22
//!
3-
//! Example:
3+
//! # Examples:
44
//!
5+
//! There are multiple ways to create a new [`Duration`]:
6+
//!
7+
//! ```
8+
//! # use std::time::Duration;
9+
//! let five_seconds = Duration::from_secs(5);
10+
//! assert_eq!(five_seconds, Duration::from_millis(5_000));
11+
//! assert_eq!(five_seconds, Duration::from_micros(5_000_000));
12+
//! assert_eq!(five_seconds, Duration::from_nanos(5_000_000_000));
13+
//!
14+
//! let ten_seconds = Duration::from_secs(10);
15+
//! let seven_nanos = Duration::from_nanos(7);
16+
//! let total = ten_seconds + seven_nanos;
17+
//! assert_eq!(total, Duration::new(10, 7));
518
//! ```
6-
//! use std::time::Duration;
719
//!
8-
//! let five_seconds = Duration::new(5, 0);
9-
//! // both declarations are equivalent
10-
//! assert_eq!(Duration::new(5, 0), Duration::from_secs(5));
20+
//! Using [`Instant`] to calculate how long a function took to run:
21+
//!
22+
//! ```ignore (incomplete)
23+
//! let now = Instant::now();
24+
//!
25+
//! // Calling a slow function, it may take a while
26+
//! slow_function();
27+
//!
28+
//! let elapsed_time = now.elapsed();
29+
//! println!("Running slow_function() took {} seconds.", elapsed_time.as_secs());
1130
//! ```
1231
1332
#![stable(feature = "time", since = "1.3.0")]
@@ -26,7 +45,7 @@ use crate::sys_common::FromInner;
2645
pub use core::time::Duration;
2746

2847
/// A measurement of a monotonically nondecreasing clock.
29-
/// Opaque and useful only with `Duration`.
48+
/// Opaque and useful only with [`Duration`].
3049
///
3150
/// Instants are always guaranteed to be no less than any previously measured
3251
/// instant when created, and are often useful for tasks such as measuring

0 commit comments

Comments
 (0)