diff --git a/ch04-03-slices.html b/ch04-03-slices.html index ca0108a85..b1d6dda12 100644 --- a/ch04-03-slices.html +++ b/ch04-03-slices.html @@ -101,12 +101,12 @@ end of the word. Let’s try that as shown in Listing 4-10:
theString 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:
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 :
for (i, &item) in bytes.iter().enumerate() {
We’ll discuss iterators in more detail in Chapter 16. For now, know that iter
@@ -115,19 +115,20 @@ the result of iter and returns each element as part of a tuple inst
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():
if item == b' ' {
return i;
}
}
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,
diff --git a/print.html b/print.html
index ef8852c5a..5177aa895 100644
--- a/print.html
+++ b/print.html
@@ -3156,12 +3156,12 @@ end of the word. Let’s try that as shown in Listing 4-10:
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:
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 :
for (i, &item) in bytes.iter().enumerate() {
We’ll discuss iterators in more detail in Chapter 16. For now, know that iter
@@ -3170,19 +3170,20 @@ the result of iter and returns each element as part of a tuple inst
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():
if item == b' ' {
return i;
}
}
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,