Skip to content

Commit 3bfc72f

Browse files
authored
Rollup merge of #41720 - frewsxcv:duration-doc-examples, r=steveklabnik
Improvements to `std::time::Duration` doc examples. Opened primarily for the last commit, in response to comments in #39949.
2 parents e2cedb2 + a3e8f36 commit 3bfc72f

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

src/libstd/time/duration.rs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,10 @@ impl Duration {
8484
/// ```
8585
/// use std::time::Duration;
8686
///
87-
/// let five_seconds = Duration::from_secs(5);
87+
/// let duration = Duration::from_secs(5);
88+
///
89+
/// assert_eq!(5, duration.as_secs());
90+
/// assert_eq!(0, duration.subsec_nanos());
8891
/// ```
8992
#[stable(feature = "duration", since = "1.3.0")]
9093
#[inline]
@@ -99,7 +102,10 @@ impl Duration {
99102
/// ```
100103
/// use std::time::Duration;
101104
///
102-
/// let five_seconds = Duration::from_millis(5000);
105+
/// let duration = Duration::from_millis(2569);
106+
///
107+
/// assert_eq!(2, duration.as_secs());
108+
/// assert_eq!(569000000, duration.subsec_nanos());
103109
/// ```
104110
#[stable(feature = "duration", since = "1.3.0")]
105111
#[inline]
@@ -119,9 +125,24 @@ impl Duration {
119125
/// ```
120126
/// use std::time::Duration;
121127
///
122-
/// let five_seconds = Duration::new(5, 0);
123-
/// assert_eq!(five_seconds.as_secs(), 5);
128+
/// let duration = Duration::new(5, 730023852);
129+
/// assert_eq!(duration.as_secs(), 5);
130+
/// ```
131+
///
132+
/// To determine the total number of seconds represented by the `Duration`,
133+
/// use `as_secs` in combination with [`subsec_nanos`]:
134+
///
124135
/// ```
136+
/// use std::time::Duration;
137+
///
138+
/// let duration = Duration::new(5, 730023852);
139+
///
140+
/// assert_eq!(5.730023852,
141+
/// duration.as_secs() as f64
142+
/// + duration.subsec_nanos() as f64 * 1e-9);
143+
/// ```
144+
///
145+
/// [`subsec_nanos`]: #method.subsec_nanos
125146
#[stable(feature = "duration", since = "1.3.0")]
126147
#[inline]
127148
pub fn as_secs(&self) -> u64 { self.secs }

0 commit comments

Comments
 (0)