From 848cb75440e4966e981efe556a66b661e0489d74 Mon Sep 17 00:00:00 2001 From: Clemens Wehrmann Date: Wed, 2 Mar 2016 19:15:22 +0100 Subject: [PATCH 1/4] Potentially surprising change to element not in focus without commentary. This is the only example where the slice starts from '1'. Since the focus here is on the last element I would avoid making readers wonder about the significance of the change to the first element, or at least remoark on it in the text. --- src/slices.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/slices.md b/src/slices.md index cc6119a7c..2a0ffdf5f 100644 --- a/src/slices.md +++ b/src/slices.md @@ -150,8 +150,8 @@ let s = String::from("hello"); let len = s.len(); -let slice = &s[1..len]; -let slice = &s[1..]; +let slice = &s[0..len]; +let slice = &s[0..]; ``` With this in mind, let’s re-write `first_word()` to return a slice: From 592c518fb6b85edeb6233f3e31513c147b9df240 Mon Sep 17 00:00:00 2001 From: Clemens Wehrmann Date: Wed, 2 Mar 2016 19:48:10 +0100 Subject: [PATCH 2/4] Example is less confusing if the assignments align content with identifiers --- src/slices.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/slices.md b/src/slices.md index 2a0ffdf5f..a3737eaa9 100644 --- a/src/slices.md +++ b/src/slices.md @@ -120,7 +120,7 @@ A string slice looks like this: let s = String::from("hello world"); let hello = &s[0..5]; -let world = &s[5..9]; +let world = &s[6..11]; ``` This looks just like taking a reference to the whole `String`, but with the From a90ac1e0cac62169776ef619b4bf1ac39aeea440 Mon Sep 17 00:00:00 2001 From: Clemens Wehrmann Date: Wed, 2 Mar 2016 19:51:40 +0100 Subject: [PATCH 3/4] Something (perhaps not this) should be said about the syntax. --- src/slices.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/slices.md b/src/slices.md index a3737eaa9..b39a70ada 100644 --- a/src/slices.md +++ b/src/slices.md @@ -131,6 +131,11 @@ it looks like this: DIAGRAM GOES HERE of s, hello, and world +Note that the internal position is specified through byte-offsets, not +characters. The offset to the first byte of a `String` is 0 and the +trailing number should point to the first byte that is _not_ included +in the slice. + With Rust’s `..` syntax, if you want to start at zero, you can drop the zero. In other words, these are equal: From 5205a78f2aa0ae2f2af12160395500497a58bcef Mon Sep 17 00:00:00 2001 From: Clemens Wehrmann Date: Wed, 2 Mar 2016 19:58:43 +0100 Subject: [PATCH 4/4] More clear to me than 'maximum value'. Difficult talking about elements since array slices are discussed later and Strings have confusing elements (byte/char). --- src/slices.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/slices.md b/src/slices.md index b39a70ada..90e1b23ac 100644 --- a/src/slices.md +++ b/src/slices.md @@ -146,8 +146,8 @@ let slice = &s[0..2]; let slice = &s[..2]; ``` -By the same token, if you want to go to the maximum value, which for slices is -the last element, you can drop the trailing number. In other words, these are +By the same token, if your slice should include the last byte of the +`String`, you can drop the trailing number. In other words, these are equal: ```rust