mirror of
https://github.com/rust-lang/book.git
synced 2026-11-30 19:24:32 -06:00
rebuild pages at 2bb8546
This commit is contained in:
+9
-5
@@ -2588,7 +2588,7 @@ around which need to be kept in sync.</p>
|
||||
<pre><code class="language-rust">let s = String::from("hello world");
|
||||
|
||||
let hello = &s[0..5];
|
||||
let world = &s[5..9];
|
||||
let world = &s[6..11];
|
||||
</code></pre>
|
||||
<p>This looks just like taking a reference to the whole <code>String</code>, but with the
|
||||
extra <code>[0..5]</code> bit. Instead of being a reference to the entire <code>String</code>,
|
||||
@@ -2596,6 +2596,10 @@ it’s a reference to an internal position in the <code>String</code>, but it al
|
||||
track of the number of elements that it refers to as well. In other words,
|
||||
it looks like this:</p>
|
||||
<p>DIAGRAM GOES HERE of s, hello, and world</p>
|
||||
<p>Note that the internal position is specified through byte-offsets, not
|
||||
characters. The offset to the first byte of a <code>String</code> is 0 and the
|
||||
trailing number should point to the first byte that is <em>not</em> included
|
||||
in the slice.</p>
|
||||
<p>With Rust’s <code>..</code> syntax, if you want to start at zero, you can drop the zero.
|
||||
In other words, these are equal:</p>
|
||||
<pre><code class="language-rust">let s = String::from("hello");
|
||||
@@ -2603,15 +2607,15 @@ In other words, these are equal:</p>
|
||||
let slice = &s[0..2];
|
||||
let slice = &s[..2];
|
||||
</code></pre>
|
||||
<p>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
|
||||
<p>By the same token, if your slice should include the last byte of the
|
||||
<code>String</code>, you can drop the trailing number. In other words, these are
|
||||
equal:</p>
|
||||
<pre><code class="language-rust">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..];
|
||||
</code></pre>
|
||||
<p>With this in mind, let’s re-write <code>first_word()</code> to return a slice:</p>
|
||||
<pre><code class="language-rust">fn first_word(s: &String) -> &str {
|
||||
|
||||
+9
-5
@@ -166,7 +166,7 @@ around which need to be kept in sync.</p>
|
||||
<pre><code class="language-rust">let s = String::from("hello world");
|
||||
|
||||
let hello = &s[0..5];
|
||||
let world = &s[5..9];
|
||||
let world = &s[6..11];
|
||||
</code></pre>
|
||||
<p>This looks just like taking a reference to the whole <code>String</code>, but with the
|
||||
extra <code>[0..5]</code> bit. Instead of being a reference to the entire <code>String</code>,
|
||||
@@ -174,6 +174,10 @@ it’s a reference to an internal position in the <code>String</code>, but it al
|
||||
track of the number of elements that it refers to as well. In other words,
|
||||
it looks like this:</p>
|
||||
<p>DIAGRAM GOES HERE of s, hello, and world</p>
|
||||
<p>Note that the internal position is specified through byte-offsets, not
|
||||
characters. The offset to the first byte of a <code>String</code> is 0 and the
|
||||
trailing number should point to the first byte that is <em>not</em> included
|
||||
in the slice.</p>
|
||||
<p>With Rust’s <code>..</code> syntax, if you want to start at zero, you can drop the zero.
|
||||
In other words, these are equal:</p>
|
||||
<pre><code class="language-rust">let s = String::from("hello");
|
||||
@@ -181,15 +185,15 @@ In other words, these are equal:</p>
|
||||
let slice = &s[0..2];
|
||||
let slice = &s[..2];
|
||||
</code></pre>
|
||||
<p>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
|
||||
<p>By the same token, if your slice should include the last byte of the
|
||||
<code>String</code>, you can drop the trailing number. In other words, these are
|
||||
equal:</p>
|
||||
<pre><code class="language-rust">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..];
|
||||
</code></pre>
|
||||
<p>With this in mind, let’s re-write <code>first_word()</code> to return a slice:</p>
|
||||
<pre><code class="language-rust">fn first_word(s: &String) -> &str {
|
||||
|
||||
Reference in New Issue
Block a user