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 https://github.com/rust-lang/rust/issues/39949.
This commit is contained in:
Corey Farwell 2017-05-03 18:34:02 -04:00 committed by GitHub
commit 3bfc72fba6

View File

@ -84,7 +84,10 @@ impl Duration {
/// ```
/// use std::time::Duration;
///
/// let five_seconds = Duration::from_secs(5);
/// let duration = Duration::from_secs(5);
///
/// assert_eq!(5, duration.as_secs());
/// assert_eq!(0, duration.subsec_nanos());
/// ```
#[stable(feature = "duration", since = "1.3.0")]
#[inline]
@ -99,7 +102,10 @@ impl Duration {
/// ```
/// use std::time::Duration;
///
/// let five_seconds = Duration::from_millis(5000);
/// let duration = Duration::from_millis(2569);
///
/// assert_eq!(2, duration.as_secs());
/// assert_eq!(569000000, duration.subsec_nanos());
/// ```
#[stable(feature = "duration", since = "1.3.0")]
#[inline]
@ -119,9 +125,24 @@ impl Duration {
/// ```
/// use std::time::Duration;
///
/// let five_seconds = Duration::new(5, 0);
/// assert_eq!(five_seconds.as_secs(), 5);
/// let duration = Duration::new(5, 730023852);
/// assert_eq!(duration.as_secs(), 5);
/// ```
///
/// To determine the total number of seconds represented by the `Duration`,
/// use `as_secs` in combination with [`subsec_nanos`]:
///
/// ```
/// use std::time::Duration;
///
/// let duration = Duration::new(5, 730023852);
///
/// assert_eq!(5.730023852,
/// duration.as_secs() as f64
/// + duration.subsec_nanos() as f64 * 1e-9);
/// ```
///
/// [`subsec_nanos`]: #method.subsec_nanos
#[stable(feature = "duration", since = "1.3.0")]
#[inline]
pub fn as_secs(&self) -> u64 { self.secs }