From 9bf73d24d0fdc4f6dbca702ec85c5583abc921cd Mon Sep 17 00:00:00 2001 From: srinivasreddy Date: Wed, 2 Mar 2016 09:36:30 +0530 Subject: [PATCH 1/4] Explained the difference between ownership iteration and reference iteration --- src/doc/book/vectors.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/doc/book/vectors.md b/src/doc/book/vectors.md index f5a543d75b1b4..0eba4d40ede01 100644 --- a/src/doc/book/vectors.md +++ b/src/doc/book/vectors.md @@ -114,7 +114,30 @@ for i in v { println!("Take ownership of the vector and its element {}", i); } ``` +Note: You cannot use the vector again once you have iterated with ownership of the vector. +You can iterate the vector multiple times with reference iteration. For example, the following +code does not compile. +```rust +let mut v = vec![1, 2, 3, 4, 5]; +for i in v { + println!("Take ownership of the vector and its element {}", i); +} +for i in v { + println!("Take ownership of the vector and its element {}", i); +} +``` +Whereas the following works perfectly, +```rust +let mut v = vec![1, 2, 3, 4, 5]; +for i in &v { + println!("A mutable reference to {}", i); +} + +for i in &v { + println!("A mutable reference to {}", i); +} +``` Vectors have many more useful methods, which you can read about in [their API documentation][vec]. From a3c9afa841aba127edac2bb60e2f2a720a51d8ac Mon Sep 17 00:00:00 2001 From: srinivasreddy Date: Wed, 2 Mar 2016 20:31:26 +0530 Subject: [PATCH 2/4] addressed review comments - grammar corrections, space additions --- src/doc/book/vectors.md | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/doc/book/vectors.md b/src/doc/book/vectors.md index 0eba4d40ede01..d9379117c86b8 100644 --- a/src/doc/book/vectors.md +++ b/src/doc/book/vectors.md @@ -114,30 +114,37 @@ for i in v { println!("Take ownership of the vector and its element {}", i); } ``` -Note: You cannot use the vector again once you have iterated with ownership of the vector. -You can iterate the vector multiple times with reference iteration. For example, the following -code does not compile. + +Note: You cannot use the vector again once you have iterated by taking ownership of the vector. +You can iterate the vector multiple times by taking a reference to the vector whilst iterating. +For example, the following code does not compile. + ```rust let mut v = vec![1, 2, 3, 4, 5]; + for i in v { println!("Take ownership of the vector and its element {}", i); } + for i in v { println!("Take ownership of the vector and its element {}", i); } ``` + Whereas the following works perfectly, ```rust let mut v = vec![1, 2, 3, 4, 5]; + for i in &v { - println!("A mutable reference to {}", i); + println!("This is a reference to {}", i); } for i in &v { - println!("A mutable reference to {}", i); + println!("This is a reference {}", i); } ``` + Vectors have many more useful methods, which you can read about in [their API documentation][vec]. From 2dc723de9a1f842fad084791cbe6029b710cf93b Mon Sep 17 00:00:00 2001 From: srinivasreddy Date: Wed, 2 Mar 2016 20:42:26 +0530 Subject: [PATCH 3/4] made print message similar across two loops --- src/doc/book/vectors.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/doc/book/vectors.md b/src/doc/book/vectors.md index d9379117c86b8..1c120186aabbd 100644 --- a/src/doc/book/vectors.md +++ b/src/doc/book/vectors.md @@ -141,7 +141,7 @@ for i in &v { } for i in &v { - println!("This is a reference {}", i); + println!("This is a reference to {}", i); } ``` From d2df5514c09aa0d9a648183b33b5b1ee1c6d0fa2 Mon Sep 17 00:00:00 2001 From: srinivasreddy Date: Thu, 3 Mar 2016 21:54:21 +0530 Subject: [PATCH 4/4] added ignore --- src/doc/book/vectors.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/doc/book/vectors.md b/src/doc/book/vectors.md index 1c120186aabbd..ceb6b3c003e52 100644 --- a/src/doc/book/vectors.md +++ b/src/doc/book/vectors.md @@ -119,7 +119,7 @@ Note: You cannot use the vector again once you have iterated by taking ownership You can iterate the vector multiple times by taking a reference to the vector whilst iterating. For example, the following code does not compile. -```rust +```rust,ignore let mut v = vec![1, 2, 3, 4, 5]; for i in v {