Skip to content

Commit a3b9b42

Browse files
committed
Rollup merge of #32452 - GuillaumeGomez:patch-5, r=steveklabnik
Add code examples for libstd/time Fixes #29379. r? @steveklabnik
2 parents 7a38ac8 + ca609cc commit a3b9b42

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

src/libstd/time/mod.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@
99
// except according to those terms.
1010

1111
//! Temporal quantification.
12+
//!
13+
//! Example:
14+
//!
15+
//! ```
16+
//! use std::time::Duration;
17+
//!
18+
//! let five_seconds = Duration::new(5, 0);
19+
//! // both declarations are equivalent
20+
//! assert_eq!(Duration::new(5, 0), Duration::from_secs(5));
21+
//! ```
1222
1323
#![stable(feature = "time", since = "1.3.0")]
1424

@@ -40,6 +50,22 @@ mod duration;
4050
/// no method to get "the number of seconds" from an instant. Instead, it only
4151
/// allows measuring the duration between two instants (or comparing two
4252
/// instants).
53+
///
54+
/// Example:
55+
///
56+
/// ```no_run
57+
/// use std::time::{Duration, Instant};
58+
/// use std::thread::sleep;
59+
///
60+
/// fn main() {
61+
/// let now = Instant::now();
62+
///
63+
/// // we sleep for 2 seconds
64+
/// sleep(Duration::new(2, 0));
65+
/// // it prints '2'
66+
/// println!("{}", now.elapsed().as_secs());
67+
/// }
68+
/// ```
4369
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
4470
#[stable(feature = "time2", since = "1.8.0")]
4571
pub struct Instant(time::Instant);
@@ -63,6 +89,30 @@ pub struct Instant(time::Instant);
6389
/// information about a `SystemTime`. By calculating the duration from this
6490
/// fixed point in time, a `SystemTime` can be converted to a human-readable time,
6591
/// or perhaps some other string representation.
92+
///
93+
/// Example:
94+
///
95+
/// ```no_run
96+
/// use std::time::{Duration, SystemTime};
97+
/// use std::thread::sleep;
98+
///
99+
/// fn main() {
100+
/// let now = SystemTime::now();
101+
///
102+
/// // we sleep for 2 seconds
103+
/// sleep(Duration::new(2, 0));
104+
/// match now.elapsed() {
105+
/// Ok(elapsed) => {
106+
/// // it prints '2'
107+
/// println!("{}", elapsed.as_secs());
108+
/// }
109+
/// Err(e) => {
110+
/// // an error occured!
111+
/// println!("Error: {:?}", e);
112+
/// }
113+
/// }
114+
/// }
115+
/// ```
66116
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
67117
#[stable(feature = "time2", since = "1.8.0")]
68118
pub struct SystemTime(time::SystemTime);

0 commit comments

Comments
 (0)