From 5283f0747943ed25db938d64f391d29c71da69e0 Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Sun, 3 Jul 2016 20:18:01 -0400 Subject: [PATCH] Resolve comments in functions Fix some parts that didn't make sense anymore since I moved data structures before this section-- tuples and destructuring have already been defined. --- src/ch03-05-how-functions-work-in-rust.md | 245 ++-------------------- 1 file changed, 22 insertions(+), 223 deletions(-) diff --git a/src/ch03-05-how-functions-work-in-rust.md b/src/ch03-05-how-functions-work-in-rust.md index e3f786b15..07a9483df 100644 --- a/src/ch03-05-how-functions-work-in-rust.md +++ b/src/ch03-05-how-functions-work-in-rust.md @@ -2,13 +2,13 @@ Functions are pervasive in Rust code. We’ve already seen one of the most important functions in the language: the `main()` function that’s the entry -point of every program. We've also seen the `fn` keyword, which allows us to +point of many programs. We've also seen the `fn` keyword, which allows us to declare new functions. Rust code uses *snake case* as the conventional style for function names. In snake case, all letters are lower case, and there are underscores separating words. (Rust also uses snake case for the names of variable bindings; we just -haven't used any variable bindings long enough to need underscores yet.) Here's +haven't used any variable bindings long enough to need underscores yet). Here's a program containing an example function definition: ```rust @@ -23,11 +23,9 @@ fn another_function() { } ``` -Function definitions in Rust always start with `fn` and have a set of parentheses after the function name. The curly -braces tell the compiler where the function body begins and ends. +Function definitions in Rust start with `fn` and have a set of parentheses +after the function name. The curly braces tell the compiler where the function +body begins and ends. We can call any function we’ve defined by entering its name followed by a pair of parentheses. Since `another_function()` is defined in the program, it can be @@ -95,20 +93,8 @@ fn NAME(PATTERN: TYPE) { ``` The parameter declaration in a single-argument function signature looks like -the `let` bindings we used earlier. Just look at both together, and compare -them: - - +the `let` bindings we used earlier in the "Type Inference and Annotation" +section. Just look at both together, and compare them: ```rust,ignore let x: i32; @@ -142,7 +128,7 @@ fn another_function(x: i32, y: i32) { } ``` -In this example, we make a function with two arguments. In this case, both are +In this example, we make a function with two arguments, both of which are `i32`s. If your function has multiple arguments, they don’t need to be the same type, but they just happen to be in this example. Our function then prints out the values of both of its arguments. @@ -181,7 +167,7 @@ fn another_function(x: i32, y: i32) { ``` Instead of passing `5` and `6` directly, this first creates two bindings -containing the values, and passes those bindings instead. When you run this, +containing the values and passes those bindings instead. When you run this, you'll find that it has the same effect as just using integers: ```bash @@ -211,13 +197,6 @@ functions. #### Statements and Expressions - - - We've already been using both statements and expressions. *Statements* are instructions that perform some action and do not return a value. *Expressions* evaluate to a resulting value. Let's look at some examples. @@ -248,34 +227,14 @@ If we were to run this program, we’d get an error like this: ```bash $ cargo run Compiling functions v0.1.0 (file:///projects/functions) -src/main.rs:2:14: 2:17 error: expected identifier, found keyword `let` +src/main.rs:2:14: 2:17 error: expected expression, found statement (`let`) src/main.rs:2 let x = (let y = 6); ^~~ -src/main.rs:2:18: 2:19 error: expected one of `!`, `)`, `,`, `.`, `::`, `{`, or an operator, found `y` -src/main.rs:2 let x = (let y = 6); - ^ -Could not compile `functions`. -``` - - - - The `let y = 6` statement does not return a value, so there isn't anything for `x` to bind to. This is different than in other languages like C and Ruby where the assignment returns the value of the assignment. In those languages, you @@ -327,48 +286,12 @@ semicolons while expressions do not. If you add a semicolon to the end of an expression, that will turn it into a statement, which will then not return a value. Keep this in mind as we explore function return values and expressions. - - ### Functions with Return Values - - - Functions can return values back to the code that calls them. In Rust, the "return value of the function” is synonymous with “the value of the final expression in the block of the body of a function.” A function that returns a value looks like this: - - ```text fn NAME(PATTERN: TYPE, PATTERN: TYPE, PATTERN: TYPE, PATTERN: TYPE...) -> TYPE { @@ -420,9 +343,9 @@ let x = 5; ``` The second interesting bit is the `five()` function itself. It requires no -arguments and defines the type of the return, but the body of the function is a -lonely `5` with no semicolon because it is an expression whose value we want to -return. Let's look at another example: +arguments and defines the type of the return value, but the body of the +function is a lonely `5` with no semicolon because it is an expression whose +value we want to return. Let's look at another example: ```rust fn main() { @@ -459,17 +382,16 @@ $ cargo run Compiling functions v0.1.0 (file:///projects/functions) src/main.rs:7:1: 9:2 error: not all control paths return a value [E0269] src/main.rs:7 fn plus_one(x: i32) -> i32 { -src/main.rs:8 x + 1; -src/main.rs:9 } + ^ src/main.rs:7:1: 9:2 help: run `rustc --explain E0269` to see a detailed explanation src/main.rs:8:10: 8:11 help: consider removing this semicolon: src/main.rs:8 x + 1; ^ error: aborting due to previous error -Could not compile `functions`. +error: Could not compile `functions`. ``` -The main error message, "not all control paths return a value," reveals the +The main error message, "not all control paths return a value", reveals the core of the issue with this code. The definition of the function `plus_one` says that it will return an `i32`, but statements don’t evaluate to a value. Therefore, nothing is returned, which contradicts the function definition and @@ -478,20 +400,8 @@ suggests removing the semicolon, which would fix the error. #### Returning Multiple Values -By default, functions can only return single values. There’s a trick, however -to get them to return multiple values. Remember how we used `()`s to create -complex bindings in the "Creating Multiple Bindings" section on page XX? - -```rust -fn main() { - let (x, y) = (5, 6); -} -``` - -Parentheses used in this way form a *tuple*-- a collection of elements that -isn't assigned a name. Tuples are also a basic data type in Rust, and we'll -cover them in detail in the "Tuples" section later in this chapter. For our -purposes now, we can use tuples to return multiple values from functions, as so: +By default, functions can only return single values. There’s a trick, however, +to get them to return multiple values: group them into a tuple! ```rust fn main() { @@ -518,118 +428,7 @@ The value of y is: 6 Let's look at this more closely. First, we're assigning the return value of calling `two_numbers()` to `x` and `y`. In the function signature, we would say -in plain English that the return type `(i32, i32)` translates to “a tuple with +in plain English that the return type `(i32, i32)` translates to "a tuple with two `i32`s in it". These two types are then applied to the tuple to be returned by the function block. In this case, that tuple contains the values `5` and -`6`. This tuple is returned, and assigned to `x` and `y`. - -See how all these bits fit together? We call this behavior of `let` -‘destructuring’, because it takes the structure of the expression that follows -the `=` and takes it apart. - - - - - +`6`. This tuple is returned, and we destructure the tuple and assign the individual values to `x` and `y`.