]> git.proxmox.com Git - rustc.git/blobdiff - src/doc/book/vectors.md
Imported Upstream version 1.9.0+dfsg1
[rustc.git] / src / doc / book / vectors.md
index f5a543d75b1b40baecb74f5605172b36157a4c0c..75e961e4c4a80504d6c2fefc27db9ebd6025d39e 100644 (file)
@@ -56,8 +56,8 @@ v[j];
 Indexing with a non-`usize` type gives an error that looks like this:
 
 ```text
-error: the trait `core::ops::Index<i32>` is not implemented for the type
-`collections::vec::Vec<_>` [E0277]
+error: the trait bound `collections::vec::Vec<_> : core::ops::Index<i32>`
+is not satisfied [E0277]
 v[j];
 ^~~~
 note: the type `collections::vec::Vec<_>` cannot be indexed by `i32`
@@ -115,6 +115,36 @@ for i in v {
 }
 ```
 
+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,ignore
+let 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 v = vec![1, 2, 3, 4, 5];
+
+for i in &v {
+    println!("This is a reference to {}", i);
+}
+
+for i in &v {
+    println!("This is a reference to {}", i);
+}
+```
+
 Vectors have many more useful methods, which you can read about in [their
 API documentation][vec].