Skip to content

Explained the difference between ownership iteration and reference iteration #32002

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 5, 2016
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/doc/book/vectors.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

iterated by taking ownership of the vector

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Also, please put a newline above this line as well)

You can iterate the vector multiple times with reference iteration. For example, the following
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

by taking a reference to the vector whilst iterating

code does not compile.
```rust
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blank line above here please!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you need to have rust,ignore here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Manishearth Done. Please issue auto merge

let mut v = vec![1, 2, 3, 4, 5];
for i in v {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and between this let and for

println!("Take ownership of the vector and its element {}", i);
}
for i in v {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and between these two fors

println!("Take ownership of the vector and its element {}", i);
}
```
Whereas the following works perfectly,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and above here


```rust
let mut v = vec![1, 2, 3, 4, 5];
for i in &v {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and between this let and for

println!("A mutable reference to {}", i);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's not mutable in this case. Just say "This is a reference"

}

for i in &v {
println!("A mutable reference to {}", i);
}
```
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and below here

Vectors have many more useful methods, which you can read about in [their
API documentation][vec].

Expand Down