From 9e825fbd59d3aaf2f00440820a1e0bf0a66bfb6a Mon Sep 17 00:00:00 2001 From: Dominik Maier Date: Mon, 17 Jul 2023 18:32:49 +0200 Subject: [PATCH] Clarify the "Slices" question and fix the answer (#990) Clarify the question and fix the answer --- src/basic-syntax/slices.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/basic-syntax/slices.md b/src/basic-syntax/slices.md index 1b9f21ba..edab4573 100644 --- a/src/basic-syntax/slices.md +++ b/src/basic-syntax/slices.md @@ -8,12 +8,13 @@ fn main() { println!("a: {a:?}"); let s: &[i32] = &a[2..4]; + println!("s: {s:?}"); } ``` * Slices borrow data from the sliced type. -* Question: What happens if you modify `a[3]`? +* Question: What happens if you modify `a[3]` right before printing `s`?
@@ -30,7 +31,6 @@ fn main() { * Slices always borrow from another object. In this example, `a` has to remain 'alive' (in scope) for at least as long as our slice. * The question about modifying `a[3]` can spark an interesting discussion, but the answer is that for memory safety reasons - you cannot do it through `a` after you created a slice, but you can read the data from both `a` and `s` safely. - More details will be explained in the borrow checker section. - + you cannot do it through `a` at this point in the execution, but you can read the data from both `a` and `s` safely. + It works before you created the slice, and again after the `println`, when the slice is no longer used. More details will be explained in the borrow checker section.