From 377ae44cf276599a5b7a21e545d83372067db754 Mon Sep 17 00:00:00 2001 From: Matthew Piziak Date: Mon, 15 Aug 2016 18:46:19 -0400 Subject: [PATCH 1/3] explicitly show how iterating over `..` fails I've also removed the `main()` wrapper, which I believe is extraneous. LMK if that's incorrect. --- src/libcore/ops.rs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/libcore/ops.rs b/src/libcore/ops.rs index 9347ac2a8c82f..558b78a2fbfaa 100644 --- a/src/libcore/ops.rs +++ b/src/libcore/ops.rs @@ -1463,15 +1463,17 @@ pub trait IndexMut: Index { /// # Examples /// /// ``` -/// fn main() { -/// assert_eq!((..), std::ops::RangeFull); +/// assert_eq!((..), std::ops::RangeFull); /// -/// let arr = [0, 1, 2, 3]; -/// assert_eq!(arr[ .. ], [0,1,2,3]); // RangeFull -/// assert_eq!(arr[ ..3], [0,1,2 ]); -/// assert_eq!(arr[1.. ], [ 1,2,3]); -/// assert_eq!(arr[1..3], [ 1,2 ]); -/// } +/// // for i in .. { +/// // println!("This errors because .. has no Iterator impl"); +/// // } +/// +/// let arr = [0, 1, 2, 3]; +/// assert_eq!(arr[ .. ], [0,1,2,3]); // RangeFull +/// assert_eq!(arr[ ..3], [0,1,2 ]); +/// assert_eq!(arr[1.. ], [ 1,2,3]); +/// assert_eq!(arr[1..3], [ 1,2 ]); /// ``` #[derive(Copy, Clone, PartialEq, Eq, Hash)] #[stable(feature = "rust1", since = "1.0.0")] From c186da706dda6ddaa4df20691702249c4ef0d2dc Mon Sep 17 00:00:00 2001 From: Matthew Piziak Date: Tue, 16 Aug 2016 06:54:45 -0400 Subject: [PATCH 2/3] RangeFull for-loop iteration fails because of IntoIterator Saying that "[for-loop iteration] fails because .. has no IntoIterator impl" is more direct than saying "...no Iterator impl" because for loops sugar into IntoIterator invocations. It just happens that the other Range* operators implement Iterator and rely on the fact that `IntoIterator` is implemented for `T: Iterator`. --- src/libcore/ops.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libcore/ops.rs b/src/libcore/ops.rs index 558b78a2fbfaa..cee374ccc7bc5 100644 --- a/src/libcore/ops.rs +++ b/src/libcore/ops.rs @@ -1466,7 +1466,7 @@ pub trait IndexMut: Index { /// assert_eq!((..), std::ops::RangeFull); /// /// // for i in .. { -/// // println!("This errors because .. has no Iterator impl"); +/// // println!("This errors because .. has no IntoIterator impl"); /// // } /// /// let arr = [0, 1, 2, 3]; From 469b7fd1c09e5759f8cbce0f4e42be91c6f1b81a Mon Sep 17 00:00:00 2001 From: Matthew Piziak Date: Thu, 18 Aug 2016 16:12:40 -0400 Subject: [PATCH 3/3] split example into three sections with explanation --- src/libcore/ops.rs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/libcore/ops.rs b/src/libcore/ops.rs index cee374ccc7bc5..932e5f086cbbe 100644 --- a/src/libcore/ops.rs +++ b/src/libcore/ops.rs @@ -1462,13 +1462,24 @@ pub trait IndexMut: Index { /// /// # Examples /// +/// The `..` syntax is a `RangeFull`: +/// /// ``` /// assert_eq!((..), std::ops::RangeFull); +/// ``` /// -/// // for i in .. { -/// // println!("This errors because .. has no IntoIterator impl"); -/// // } +/// It does not have an `IntoIterator` implementation, so you can't use it in a +/// `for` loop directly. This won't compile: /// +/// ```ignore +/// for i in .. { +/// // ... +/// } +/// ``` +/// +/// Used as a slicing index, `RangeFull` produces the full array as a slice. +/// +/// ``` /// let arr = [0, 1, 2, 3]; /// assert_eq!(arr[ .. ], [0,1,2,3]); // RangeFull /// assert_eq!(arr[ ..3], [0,1,2 ]);