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 1/6] 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!"); } From fd0c2c50c1a145e746735af7751919814f22a52a Mon Sep 17 00:00:00 2001 From: mckzm <134839822+mckzm@users.noreply.github.com> Date: Sat, 15 Jun 2024 13:39:12 +0900 Subject: [PATCH 2/6] update vector/slicing exercise solution w/ mutable slice material --- solutions/collections/Vector.md | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/solutions/collections/Vector.md b/solutions/collections/Vector.md index 7581a80..68edfba 100644 --- a/solutions/collections/Vector.md +++ b/solutions/collections/Vector.md @@ -156,21 +156,24 @@ fn main() { let mut v = vec![1, 2, 3]; let slice1 = &v[..]; - // out of bounds will cause a panic + // Out of bounds will cause a panic // You must use `v.len` here - let slice2 = &v[0..v.len()]; + let slice2 = &v[0..3]; assert_eq!(slice1, slice2); - // slice are read only + // A slice can also be mutable, in which + // case mutating it will also mutate its underlying Vec // Note: slice and &Vec are different let vec_ref: &mut Vec = &mut v; (*vec_ref).push(4); - let slice3 = &mut v[0..]; + let slice3 = &mut v[0..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!") + println!("Success!"); } ``` From a637487fad89e160f887f24eaf018ef316fcba5b Mon Sep 17 00:00:00 2001 From: mckzm <134839822+mckzm@users.noreply.github.com> Date: Sat, 15 Jun 2024 13:40:17 +0900 Subject: [PATCH 3/6] vector/slicing - improve the phrasing --- en/src/collections/vector.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/en/src/collections/vector.md b/en/src/collections/vector.md index ea16799..bf21c7a 100644 --- a/en/src/collections/vector.md +++ b/en/src/collections/vector.md @@ -129,8 +129,8 @@ fn main() { assert_eq!(slice1, slice2); - // Slices can also be mutable, in which - // case mutating them will mutate the underlying Vec + // A slice can also be mutable, in which + // case mutating it will mutate its underlying Vec // Note: slice and &Vec are different let vec_ref: &mut Vec = &mut v; (*vec_ref).push(4); From 0120c3a97916a5a18ba6d3412f18d6763ea55362 Mon Sep 17 00:00:00 2001 From: mckzm <134839822+mckzm@users.noreply.github.com> Date: Sat, 15 Jun 2024 13:44:11 +0900 Subject: [PATCH 4/6] Further clarification on the benefits of passing in immutable slices to functions --- en/src/collections/vector.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/en/src/collections/vector.md b/en/src/collections/vector.md index bf21c7a..6b19134 100644 --- a/en/src/collections/vector.md +++ b/en/src/collections/vector.md @@ -113,7 +113,7 @@ fn main() { ### Slicing Immutable or mutable slices of Vecs can be taken, using `&` or `&mut, respectively. -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`. +In Rust, itโ€™s more common to pass immutable slices as arguments rather than vectors when you just want to provide read access, as this is more flexible (no move) and efficient (no copy). The same goes for `String` and `&str`. 5. ๐ŸŒŸ๐ŸŒŸ ```rust,editable From e7cfb63ac88d1a848667a890fce4655d80fbf585 Mon Sep 17 00:00:00 2001 From: mckzm <134839822+mckzm@users.noreply.github.com> Date: Sat, 15 Jun 2024 14:18:14 +0900 Subject: [PATCH 5/6] fix minor typo in vector.md --- en/src/collections/vector.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/en/src/collections/vector.md b/en/src/collections/vector.md index 6b19134..e9de6f8 100644 --- a/en/src/collections/vector.md +++ b/en/src/collections/vector.md @@ -111,7 +111,7 @@ fn main() { ### Slicing -Immutable or mutable slices of Vecs can be taken, using `&` or `&mut, respectively. +Immutable or mutable slices of Vecs can be taken, using `&` or `&mut`, respectively. In Rust, itโ€™s more common to pass immutable slices as arguments rather than vectors when you just want to provide read access, as this is more flexible (no move) and efficient (no copy). The same goes for `String` and `&str`. From cd1e8a242ef1a5a3fa492a6031b6a47b3fac9856 Mon Sep 17 00:00:00 2001 From: Sunface Date: Thu, 27 Jun 2024 17:02:47 +0800 Subject: [PATCH 6/6] Update en/src/collections/vector.md --- en/src/collections/vector.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/en/src/collections/vector.md b/en/src/collections/vector.md index e9de6f8..b416eec 100644 --- a/en/src/collections/vector.md +++ b/en/src/collections/vector.md @@ -130,7 +130,7 @@ fn main() { assert_eq!(slice1, slice2); // A slice can also be mutable, in which - // case mutating it will mutate its underlying Vec + // case mutating it will mutate its underlying Vec. // Note: slice and &Vec are different let vec_ref: &mut Vec = &mut v; (*vec_ref).push(4);