From c45473d2b7b4df65cd519abff8c767159038946c Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Mon, 10 Oct 2016 18:12:44 -0400 Subject: [PATCH] Make calculate_length more idiomatic. Fixes #76. --- src/ch04-02-references-and-borrowing.md | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/src/ch04-02-references-and-borrowing.md b/src/ch04-02-references-and-borrowing.md index 93cb60aa2..9ff5b9e25 100644 --- a/src/ch04-02-references-and-borrowing.md +++ b/src/ch04-02-references-and-borrowing.md @@ -21,9 +21,7 @@ fn main() { } fn calculate_length(s: &String) -> usize { - let length = s.len(); - - length + s.len() } ``` @@ -43,9 +41,7 @@ Let’s take a closer look at the function call here: ```rust # fn calculate_length(s: &String) -> usize { -# let length = s.len(); -# -# length +# s.len() # } let s1 = String::from("hello"); @@ -61,9 +57,7 @@ a reference as an argument. Let’s add some explanatory annotations: ```rust fn calculate_length(s: &String) -> usize { // s is a reference to a String - let length = s.len(); - - length + s.len() } // Here, s goes out of scope. But since it does not have ownership of what // it refers to, nothing happens. ```