]> git.proxmox.com Git - rustc.git/blobdiff - src/doc/book/second-edition/src/ch03-03-how-functions-work.md
New upstream version 1.26.0+dfsg1
[rustc.git] / src / doc / book / second-edition / src / ch03-03-how-functions-work.md
index aacd475abd6f9489611f2438613818d54b119629..5bf722342b3f4cda9689f44b60f6b6daf563395d 100644 (file)
@@ -1,4 +1,4 @@
-## How Functions Work
+## Functions
 
 Functions are pervasive in Rust code. You’ve already seen one of the most
 important functions in the language: the `main` function, which is the entry
@@ -54,12 +54,12 @@ called and its message is printed.
 ### Function Parameters
 
 Functions can also be defined to have *parameters*, which are special variables
-that are part of a function’s signature. When a function has parameters, we can
-provide it with concrete values for those parameters. Technically, the concrete
-values are called *arguments*, but in casual conversation people tend to use
-the words “parameter” and “argument” interchangeably for either the variables
-in a function’s definition or the concrete values passed in when you call a
-function.
+that are part of a function’s signature. When a function has parameters, you
+can provide it with concrete values for those parameters. Technically, the
+concrete values are called *arguments*, but in casual conversation, people tend
+to use the words *parameter* and *argument* interchangeably for either the
+variables in a function’s definition or the concrete values passed in when you
+call a function.
 
 The following rewritten version of `another_function` shows what parameters
 look like in Rust:
@@ -113,13 +113,13 @@ fn another_function(x: i32, y: i32) {
 ```
 
 This example creates a function with two parameters, both of which are `i32`
-types. The function then prints out the values in both of its parameters. Note
-that function parameters don’t all need to be the same type, they just happen
-to be in this example.
+types. The function then prints the values in both of its parameters. Note that
+function parameters don’t all need to be the same type, they just happen to be
+in this example.
 
 Let’s try running this code. Replace the program currently in your *functions*
-project’s *src/main.rs* file with the preceding example, and run it using
-`cargo run`:
+project’s *src/main.rs* file with the preceding example and run it using `cargo
+run`:
 
 ```text
 $ cargo run
@@ -137,7 +137,7 @@ as the value for `y`, the two strings are printed with these values.
 
 Function bodies are made up of a series of statements optionally ending in an
 expression. So far, we’ve only covered functions without an ending expression,
-but we have seen expressions as parts of statements. Because Rust is an
+but you have seen an expression as part of statements. Because Rust is an
 expression-based language, this is an important distinction to understand.
 Other languages don’t have the same distinctions, so let’s look at what
 statements and expressions are and how their differences affect the bodies of
@@ -160,7 +160,7 @@ fn main() {
 }
 ```
 
-<span class="caption">Listing 3-1: A `main` function declaration containing one statement.</span>
+<span class="caption">Listing 3-1: A `main` function declaration containing one statement</span>
 
 Function definitions are also statements; the entire preceding example is a
 statement in itself.
@@ -191,15 +191,15 @@ error: expected expression, found statement (`let`)
 ```
 
 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, such as C and Ruby,
-where the assignment returns the value of the assignment. In those languages,
-you can write `x = y = 6` and have both `x` and `y` have the value `6`; that is
-not the case in Rust.
+`x` to bind to. This is different from what happens in other languages, such as
+C and Ruby, where the assignment returns the value of the assignment. In those
+languages, you can write `x = y = 6` and have both `x` and `y` have the value
+`6`; that is not the case in Rust.
 
 Expressions evaluate to something and make up most of the rest of the code that
 you’ll write in Rust. Consider a simple math operation, such as `5 + 6`, which
 is an expression that evaluates to the value `11`. Expressions can be part of
-statements: in Listing 3-1 that had the statement `let y = 6;`, `6` is an
+statements: in Listing 3-1, the `6` in the statement `let y = 6;` is an
 expression that evaluates to the value `6`. Calling a function is an
 expression. Calling a macro is an expression. The block that we use to create
 new scopes, `{}`, is an expression, for example:
@@ -230,10 +230,10 @@ This expression:
 
 is a block that, in this case, evaluates to `4`. That value gets bound to `y`
 as part of the `let` statement. Note the `x + 1` line without a semicolon at
-the end, unlike most of the lines you’ve seen so far. Expressions do not
-include ending semicolons. If you add a semicolon to the end of an expression,
-you turn it into a statement, which will then not return a value. Keep this in
-mind as you explore function return values and expressions next.
+the end, which is unlike most of the lines you’ve seen so far. Expressions do
+not include ending semicolons. If you add a semicolon to the end of an
+expression, you turn it into a statement, which will then not return a value.
+Keep this in mind as you explore function return values and expressions next.
 
 ### Functions with Return Values
 
@@ -284,8 +284,9 @@ let x = 5;
 
 Second, the `five` function has no parameters and defines the type of the
 return value, but the body of the function is a lonely `5` with no semicolon
-because it’s an expression whose value we want to return. Let’s look at another
-example:
+because it’s an expression whose value we want to return.
+
+Let’s look at another example:
 
 <span class="filename">Filename: src/main.rs</span>
 
@@ -301,9 +302,9 @@ fn plus_one(x: i32) -> i32 {
 }
 ```
 
-Running this code will print `The value of x is: 6`. What happens if we place a
+Running this code will print `The value of x is: 6`. But if we place a
 semicolon at the end of the line containing `x + 1`, changing it from an
-expression to a statement? We’ll get an error:
+expression to a statement, we’ll get an error.
 
 <span class="filename">Filename: src/main.rs</span>