From 2e2e1a1d74830a7f23a1c272aff9f7be2e1032fa Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Sun, 11 Dec 2016 12:41:49 -0500 Subject: [PATCH] Rearrange explanatory text and code to make sense Fixes #350. --- src/ch04-03-slices.md | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/ch04-03-slices.md b/src/ch04-03-slices.md index 5a121b489..4358fd209 100644 --- a/src/ch04-03-slices.md +++ b/src/ch04-03-slices.md @@ -45,15 +45,15 @@ the `String` argument -Let’s break down this code a bit: +Let’s break down this code a bit. Because we need to go through the `String` +element by element and check whether a value is a space, we’ll convert our +`String` to an array of bytes using the `as_bytes` method: ```rust,ignore let bytes = s.as_bytes(); ``` -Because we need to go through the `String` element by element and check whether -a value is a space, we’ll convert our `String` to an array of bytes using the -`as_bytes` method: +Next, we create an iterator over the array of bytes using the `iter` method : ```rust,ignore for (i, &item) in bytes.iter().enumerate() { @@ -66,10 +66,15 @@ first element of the returned tuple is the index, and the second element is a reference to the element. This is a bit more convenient than calculating the index ourselves. -Because the method returns a tuple, we can use patterns, just like everywhere -else in Rust. So we match against the tuple with `i` for the index and `&item` -for a single byte. Because we get a reference from `.iter().enumerate()`, we -use `&` in the pattern: +Because the `enumerate` method returns a tuple, we can use patterns to +destructure that tuple, just like everywhere else in Rust. So in the `for` +loop, we specify a pattern that has `i` for the index in the tuple and `&item` +for the single byte in the tuple. Because we get a reference to the element +from `.iter().enumerate()`, we use `&` in the pattern. + +We search for the byte that represents the space by using the byte literal +syntax. If we find a space, we return the position. Otherwise, we return the +length of the string by using `s.len()`: ```rust,ignore if item == b' ' { @@ -79,10 +84,6 @@ use `&` in the pattern: s.len() ``` -We search for the byte that represents the space by using the byte literal -syntax. If we find a space, we return the position. Otherwise, we return the -length of the string by using `s.len()`. - We now have a way to find out the index of the end of the first word in the string, but there’s a problem. We’re returning a `usize` on its own, but it’s only a meaningful number in the context of the `&String`. In other words,