@@ -114,30 +114,37 @@ for i in v {
114
114
println! (" Take ownership of the vector and its element {}" , i );
115
115
}
116
116
```
117
- Note: You cannot use the vector again once you have iterated with ownership of the vector.
118
- You can iterate the vector multiple times with reference iteration. For example, the following
119
- code does not compile.
117
+
118
+ Note: You cannot use the vector again once you have iterated by taking ownership of the vector.
119
+ You can iterate the vector multiple times by taking a reference to the vector whilst iterating.
120
+ For example, the following code does not compile.
121
+
120
122
``` rust
121
123
let mut v = vec! [1 , 2 , 3 , 4 , 5 ];
124
+
122
125
for i in v {
123
126
println! (" Take ownership of the vector and its element {}" , i );
124
127
}
128
+
125
129
for i in v {
126
130
println! (" Take ownership of the vector and its element {}" , i );
127
131
}
128
132
```
133
+
129
134
Whereas the following works perfectly,
130
135
131
136
``` rust
132
137
let mut v = vec! [1 , 2 , 3 , 4 , 5 ];
138
+
133
139
for i in & v {
134
- println! (" A mutable reference to {}" , i );
140
+ println! (" This is a reference to {}" , i );
135
141
}
136
142
137
143
for i in & v {
138
- println! (" A mutable reference to {}" , i );
144
+ println! (" This is a reference {}" , i );
139
145
}
140
146
```
147
+
141
148
Vectors have many more useful methods, which you can read about in [ their
142
149
API documentation] [ vec ] .
143
150
0 commit comments