From 8e5bba01d663417e3494d58a51149b0aa834f94a Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Tue, 2 Aug 2016 17:01:48 -0400 Subject: [PATCH] Clarify this code sample with less shadowing and more comments --- src/ch04-03-slices.md | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/ch04-03-slices.md b/src/ch04-03-slices.md index 2f31638..28d9627 100644 --- a/src/ch04-03-slices.md +++ b/src/ch04-03-slices.md @@ -288,13 +288,19 @@ with no loss of functionality: # &s[..] # } fn main() { - let s = String::from("hello world"); - let word = first_word(&s[..]); + let my_string = String::from("hello world"); - let s = "hello world"; - let word = first_word(&s[..]); + // first_word works on slices of `String`s + let word = first_word(&my_string[..]); - let word = first_word(s); // since literals are &strs, this works too! + let my_string_literal = "hello world"; + + // first_word works on slices of string literals + let word = first_word(&my_string_literal[..]); + + // since string literals *are* string slices already, + // this works too, without the slice syntax! + let word = first_word(my_string_literal); } ```