From c4804e310154a577b4aeeb80978a22581dfe6b31 Mon Sep 17 00:00:00 2001 From: mckzm <134839822+mckzm@users.noreply.github.com> Date: Sat, 15 Jun 2024 13:35:27 +0900 Subject: [PATCH] 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. --- en/src/collections/vector.md | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/en/src/collections/vector.md b/en/src/collections/vector.md index 63a2594..ea16799 100644 --- a/en/src/collections/vector.md +++ b/en/src/collections/vector.md @@ -111,9 +111,9 @@ fn main() { ### Slicing -A Vec can be mutable. On the other hand, slices are read-only objects. To get a slice, use `&`. +Immutable or mutable slices of Vecs can be taken, using `&` or `&mut, respectively. -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`. +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`. 5. ๐ŸŒŸ๐ŸŒŸ ```rust,editable @@ -129,14 +129,16 @@ fn main() { assert_eq!(slice1, slice2); - // Slices are read only + // Slices can also be mutable, in which + // case mutating them will mutate the underlying Vec // Note: slice and &Vec are different let vec_ref: &mut Vec = &mut v; (*vec_ref).push(4); let slice3 = &mut v[0..3]; - slice3.push(4); + slice3[3] = 42; - assert_eq!(slice3, &[1, 2, 3, 4]); + assert_eq!(slice3, &[1, 2, 3, 42]); + assert_eq!(v, &[1, 2, 3, 42]); println!("Success!"); }