From bd7d541dbdc2280c49cd0ee148df5f004758c783 Mon Sep 17 00:00:00 2001 From: Andrew Cann Date: Fri, 24 Nov 2017 15:36:49 +0800 Subject: [PATCH] Fix doc tests --- src/libstd/primitive_docs.rs | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/src/libstd/primitive_docs.rs b/src/libstd/primitive_docs.rs index 751daac9c485c..7c3d9fd802434 100644 --- a/src/libstd/primitive_docs.rs +++ b/src/libstd/primitive_docs.rs @@ -79,9 +79,12 @@ mod prim_bool { } /// write /// /// ``` +/// # #![feature(never_type)] +/// # fn foo() -> u32 { /// let x: ! = { -/// return 123; +/// return 123 /// }; +/// # } /// ``` /// /// Although the `let` is pointless here, it illustrates the meaning of `!`. Since `x` is never @@ -92,10 +95,13 @@ mod prim_bool { } /// A more realistic usage of `!` is in this code: /// /// ``` +/// # fn get_a_number() -> Option { None } +/// # loop { /// let num: u32 = match get_a_number() { /// Some(num) => num, /// None => break, -/// } +/// }; +/// # } /// ``` /// /// Both match arms must produce values of type `u32`, but since `break` never produces a value at @@ -110,18 +116,20 @@ mod prim_bool { } /// trait: /// /// ``` -/// trait FromStr { -/// type Error; -/// fn from_str(s: &str) -> Result; +/// trait FromStr: Sized { +/// type Err; +/// fn from_str(s: &str) -> Result; /// } /// ``` /// -/// When implementing this trait for `String` we need to pick a type for `Error`. And since +/// When implementing this trait for `String` we need to pick a type for `Err`. And since /// converting a string into a string will never result in an error, the appropriate type is `!`. -/// If we have to call `String::from_str` for some reason, the result will be a -/// `Result`, which we can unpack like this: +/// (Currently the type actually used is an enum with no variants, though this is only because `!` +/// was added to Rust at a later date and it may change in the future). With an `Err` type of `!`, +/// if we have to call `String::from_str` for some reason the result will be a `Result` +/// which we can unpack like this: /// -/// ``` +/// ```ignore (string-from-str-error-type-is-not-never-yet) /// let Ok(s) = String::from_str("hello"); /// ``` /// @@ -138,6 +146,11 @@ mod prim_bool { } /// for example: /// /// ``` +/// # #![feature(never_type)] +/// # use std::fmt; +/// # trait Debug { +/// # fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result; +/// # } /// impl Debug for ! { /// fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { /// *self