diff options
| author | srinivasreddy <thatiparthysreenivas@gmail.com> | 2016-03-02 09:36:30 +0530 |
|---|---|---|
| committer | srinivasreddy <thatiparthysreenivas@gmail.com> | 2016-03-02 09:36:30 +0530 |
| commit | 9bf73d24d0fdc4f6dbca702ec85c5583abc921cd (patch) | |
| tree | 4c5ebd9aa67ae57d034f4a1f54c53cf40a753a05 /src/doc | |
| parent | 54fdae3d6eb944c80fd2b0de3130ac0416d51bf0 (diff) | |
| download | rust-9bf73d24d0fdc4f6dbca702ec85c5583abc921cd.tar.gz rust-9bf73d24d0fdc4f6dbca702ec85c5583abc921cd.zip | |
Explained the difference between ownership iteration and reference iteration
Diffstat (limited to 'src/doc')
| -rw-r--r-- | src/doc/book/vectors.md | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/src/doc/book/vectors.md b/src/doc/book/vectors.md index f5a543d75b1..0eba4d40ede 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]. |
