Skip to content

Commit c4804e3

Browse files
authored
Update Slicing subsection of Vector section
Slices can be mutable as well as immutable, contrary to what the text currently says. This commit updates the text, and the exercise, to reflect this.
1 parent 9fb6572 commit c4804e3

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

en/src/collections/vector.md

+7-5
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,9 @@ fn main() {
111111

112112

113113
### Slicing
114-
A Vec can be mutable. On the other hand, slices are read-only objects. To get a slice, use `&`.
114+
Immutable or mutable slices of Vecs can be taken, using `&` or `&mut, respectively.
115115

116-
In Rust, it’s more common to pass slices as arguments rather than vectors when you just want to provide read access. The same goes for `String` and `&str`.
116+
In Rust, it’s more common to pass immutable slices as arguments rather than vectors when you just want to provide read access. The same goes for `String` and `&str`.
117117

118118
5. 🌟🌟
119119
```rust,editable
@@ -129,14 +129,16 @@ fn main() {
129129
130130
assert_eq!(slice1, slice2);
131131
132-
// Slices are read only
132+
// Slices can also be mutable, in which
133+
// case mutating them will mutate the underlying Vec
133134
// Note: slice and &Vec are different
134135
let vec_ref: &mut Vec<i32> = &mut v;
135136
(*vec_ref).push(4);
136137
let slice3 = &mut v[0..3];
137-
slice3.push(4);
138+
slice3[3] = 42;
138139
139-
assert_eq!(slice3, &[1, 2, 3, 4]);
140+
assert_eq!(slice3, &[1, 2, 3, 42]);
141+
assert_eq!(v, &[1, 2, 3, 42]);
140142
141143
println!("Success!");
142144
}

0 commit comments

Comments
 (0)