From 08a4da055c648c640ae9b49f2dbe912c19d4e224 Mon Sep 17 00:00:00 2001 From: Steve Klabnik Date: Wed, 9 Mar 2016 21:47:09 +0000 Subject: [PATCH] rebuild pages at 2bb8546 --- print.html | 14 +++++++++----- slices.html | 14 +++++++++----- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/print.html b/print.html index 04e308b13..1714d29a8 100644 --- a/print.html +++ b/print.html @@ -2588,7 +2588,7 @@ around which need to be kept in sync.

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 extra [0..5] bit. Instead of being a reference to the entire String, @@ -2596,6 +2596,10 @@ it’s a reference to an internal position in the String, but it al track of the number of elements that it refers to as well. In other words, 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:

let s = String::from("hello");
@@ -2603,15 +2607,15 @@ In other words, these are equal:

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:

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:

fn first_word(s: &String) -> &str {
diff --git a/slices.html b/slices.html
index a69384dc3..f16c5a70b 100644
--- a/slices.html
+++ b/slices.html
@@ -166,7 +166,7 @@ around which need to be kept in sync.

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 extra [0..5] bit. Instead of being a reference to the entire String, @@ -174,6 +174,10 @@ it’s a reference to an internal position in the String, but it al track of the number of elements that it refers to as well. In other words, 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:

let s = String::from("hello");
@@ -181,15 +185,15 @@ In other words, these are equal:

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:

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:

fn first_word(s: &String) -> &str {