From 3aac461d4053e8f8479d72a3a4c2f26a1b13a5aa Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Mon, 14 Mar 2016 16:43:55 +0100 Subject: [PATCH 1/6] Add doc examples --- src/libcore/cmp.rs | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/libcore/cmp.rs b/src/libcore/cmp.rs index 042cbea64bd26..49aa0238a996a 100644 --- a/src/libcore/cmp.rs +++ b/src/libcore/cmp.rs @@ -14,6 +14,21 @@ //! by the compiler to implement comparison operators. Rust programs may //! implement `PartialOrd` to overload the `<`, `<=`, `>`, and `>=` operators, //! and may implement `PartialEq` to overload the `==` and `!=` operators. +//! +//! # Examples +//! +//! ``` +//! let x: u32 = 0; +//! let y: u32 = 1; +//! +//! // these two lines are equivalent +//! assert_eq!(x < y, true); +//! assert_eq!(x.lt(&y), true); +//! +//! // these two lines are also equivalent +//! assert_eq!(x == y, false); +//! assert_eq!(x.eq(&y), false); +//! ``` #![stable(feature = "rust1", since = "1.0.0")] @@ -44,6 +59,16 @@ use option::Option::{self, Some}; /// only if `a != b`. /// /// This trait can be used with `#[derive]`. +/// +/// # Examples +/// +/// ``` +/// let x: u32 = 0; +/// let y: u32 = 1; +/// +/// assert_eq!(x == y, false); +/// assert_eq!(x.eq(&y), false); +/// ``` #[lang = "eq"] #[stable(feature = "rust1", since = "1.0.0")] pub trait PartialEq { @@ -226,6 +251,16 @@ impl PartialOrd for Ordering { /// /// This trait can be used with `#[derive]`. When `derive`d, it will produce an ordering /// based on the top-to-bottom declaration order of the struct's members. +/// +/// # Examples +/// +/// ``` +/// let x : u32 = 0; +/// let y : u32 = 1; +/// +/// assert_eq!(x < y, true); +/// assert_eq!(x.lt(&y), true); +/// ``` #[lang = "ord"] #[stable(feature = "rust1", since = "1.0.0")] pub trait PartialOrd: PartialEq { From 2318e9fbe2caf7c2cc7bf63cf4ea244d274ba16b Mon Sep 17 00:00:00 2001 From: Tang Chenglong Date: Fri, 18 Mar 2016 23:29:12 +0800 Subject: [PATCH 2/6] docs: Make some tiny modification about spelling --- src/doc/book/primitive-types.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/doc/book/primitive-types.md b/src/doc/book/primitive-types.md index 840609d1dd629..8dfbf4ada765f 100644 --- a/src/doc/book/primitive-types.md +++ b/src/doc/book/primitive-types.md @@ -7,7 +7,7 @@ of these ones, as well, but these are the most primitive. # Booleans -Rust has a built in boolean type, named `bool`. It has two values, `true` and `false`: +Rust has a built-in boolean type, named `bool`. It has two values, `true` and `false`: ```rust let x = true; @@ -89,13 +89,13 @@ Unsigned types use a `u` for their category, and signed types use `i`. The `i` is for ‘integer’. So `u8` is an eight-bit unsigned number, and `i8` is an eight-bit signed number. -## Fixed size types +## Fixed-size types -Fixed size types have a specific number of bits in their representation. Valid +Fixed-size types have a specific number of bits in their representation. Valid bit sizes are `8`, `16`, `32`, and `64`. So, `u32` is an unsigned, 32-bit integer, and `i64` is a signed, 64-bit integer. -## Variable sized types +## Variable-size types Rust also provides types whose size depends on the size of a pointer of the underlying machine. These types have ‘size’ as the category, and come in signed From 8f99ad2a2a8a95b395de93d692f1b652721e94c8 Mon Sep 17 00:00:00 2001 From: Kevin Brothaler Date: Fri, 18 Mar 2016 11:19:20 -0400 Subject: [PATCH 3/6] Update of the book; Error handling, section on custom error types: we should also show the changes to the `cause` method. --- src/doc/book/error-handling.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/doc/book/error-handling.md b/src/doc/book/error-handling.md index 11086af10bd3d..12cb71973ab25 100644 --- a/src/doc/book/error-handling.md +++ b/src/doc/book/error-handling.md @@ -2019,6 +2019,16 @@ impl Error for CliError { CliError::NotFound => "not found", } } + + fn cause(&self) -> Option<&error::Error> { + match *self { + CliError::Io(ref err) => Some(err), + CliError::Parse(ref err) => Some(err), + // Our custom error doesn't have an underlying cause, but we could + // modify it so that it does. + CliError::NotFound() => None, + } + } } ``` From 06d8b21372742234c95e1ea842e4d66aa9ee9dd5 Mon Sep 17 00:00:00 2001 From: Tang Chenglong Date: Sun, 20 Mar 2016 21:24:46 +0800 Subject: [PATCH 4/6] docs: Correct an improper description In the example, we made a immutable borrow to `println!`, not a mutable one. --- src/doc/book/references-and-borrowing.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/doc/book/references-and-borrowing.md b/src/doc/book/references-and-borrowing.md index 74983c1255333..a08d53f958ba3 100644 --- a/src/doc/book/references-and-borrowing.md +++ b/src/doc/book/references-and-borrowing.md @@ -212,7 +212,7 @@ fn main() { In other words, the mutable borrow is held through the rest of our example. What we want is for the mutable borrow by `y` to end so that the resource can be -returned to the owner, `x`. `x` can then provide a mutable borrow to `println!`. +returned to the owner, `x`. `x` can then provide a immutable borrow to `println!`. In Rust, borrowing is tied to the scope that the borrow is valid for. And our scopes look like this: From 903bcb84a1f3a9d042810b3e04b874ebf9a12216 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cyryl=20P=C5=82otnicki-Chudyk?= Date: Sun, 20 Mar 2016 16:09:54 +0100 Subject: [PATCH 5/6] Documentation fix for E0368 Made the 'good' example compile. I got to the E0368 error page by following the link in the output of the compiler. My understanding is that the first example is 'bad' and the second one is 'good'. Following that logic, I pasted the second example into the file and to my surprise it did not compile. This commit fixes the example to make it paste-able. --- src/librustc_typeck/diagnostics.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/librustc_typeck/diagnostics.rs b/src/librustc_typeck/diagnostics.rs index 19cfc13ea615c..300868721e3ab 100644 --- a/src/librustc_typeck/diagnostics.rs +++ b/src/librustc_typeck/diagnostics.rs @@ -3167,8 +3167,8 @@ x <<= 2; To fix this error, please check that this type implements this binary operation. Example: -```compile_fail -let x = 12u32; // the `u32` type does implement the `ShlAssign` trait +``` +let mut x = 12u32; // the `u32` type does implement the `ShlAssign` trait x <<= 2; // ok! ``` From a5d9057ca7f7b08adef7cfecea2cc446ee47cb85 Mon Sep 17 00:00:00 2001 From: Tang Chenglong Date: Mon, 21 Mar 2016 21:00:04 +0800 Subject: [PATCH 6/6] docs: Make some changes in texts In my understanding, the description is somehow inappropriate. --- src/doc/book/match.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/doc/book/match.md b/src/doc/book/match.md index acffaf4544b10..1c327c376e203 100644 --- a/src/doc/book/match.md +++ b/src/doc/book/match.md @@ -36,10 +36,10 @@ give us an error: error: non-exhaustive patterns: `_` not covered ``` -Rust is telling us that we forgot a value. The compiler infers from `x` that it -can have any positive 32bit value; for example 1 to 2,147,483,647. The `_` acts +Rust is telling us that we forgot some value. The compiler infers from `x` that it +can have any 32bit integer value; for example -2,147,483,648 to 2,147,483,647. The `_` acts as a 'catch-all', and will catch all possible values that *aren't* specified in -an arm of `match`. As you can see with the previous example, we provide `match` +an arm of `match`. As you can see in the previous example, we provide `match` arms for integers 1-5, if `x` is 6 or any other value, then it is caught by `_`. `match` is also an expression, which means we can use it on the right-hand