From 4e73350c086bdf48bd903e6b05de85e39f8fcf29 Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Fri, 29 Jul 2016 10:43:26 -0700 Subject: [PATCH] Split up example for better readability --- src/ch03-03-slices.md | 43 ++++++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/src/ch03-03-slices.md b/src/ch03-03-slices.md index af466fe..889d423 100644 --- a/src/ch03-03-slices.md +++ b/src/ch03-03-slices.md @@ -37,38 +37,39 @@ fn first_word(s: &String) -> usize { Let’s break that down a bit: ```rust -fn first_word(s: &String) -> usize { - - // Since we need to go through the String element by element and - // check if a value is a space, we will convert our String to an - // array of bytes using the `.as_bytes()` method. let bytes = s.as_bytes(); +``` - // We will be discussing iterators in more detail in Chapter XX, but for - // now, know that `iter()` is a method that returns each element in a - // collection, and `enumerate()` modifies the result of `iter()` and returns - // a tuple instead. The first element of the tuple is the index, and the - // second element is a reference to the element itself. This is a bit - // nicer than calculating the index ourselves. - // - // Since it’s a tuple, we can use patterns, just like elsewhere in Rust. - // So we match against the tuple with i for the index and &byte for - // the byte itself. +Since we need to go through the String element by element and +check if a value is a space, we will convert our String to an +array of bytes using the `.as_bytes()` method. + +```rust for (i, &byte) in bytes.iter().enumerate() { +``` - // 32 is the value of a space in UTF-8 +We will be discussing iterators in more detail in Chapter XX, but for +now, know that `iter()` is a method that returns each element in a +collection, and `enumerate()` modifies the result of `iter()` and returns +a tuple instead. The first element of the tuple is the index, and the +second element is a reference to the element itself. This is a bit +nicer than calculating the index ourselves. + +Since it’s a tuple, we can use patterns, just like elsewhere in Rust. +So we match against the tuple with i for the index and &byte for +the byte itself. + +```rust if byte == 32 { - // We found a space! Return this position. return i; } } - - // If we got here, we didn’t find a space, so the whole string must be a - // word. Return the length. s.len() -} ``` +We search for the value 32, which represents a space in UTF-8. If we find one, we return the +position. Otherwise, we return the length of the string, using `s.len()`. + This works, 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, because it’s a separate value from the `String`, there’s no guarantee