From fc818ff33b8b83ec1c669660acab37fc6576eee4 Mon Sep 17 00:00:00 2001 From: Steve Klabnik Date: Wed, 1 Oct 2014 17:10:59 -0400 Subject: [PATCH 1/5] =?UTF-8?q?:fire:=20=CF=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #17674 --- src/doc/guide-lifetimes.md | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/doc/guide-lifetimes.md b/src/doc/guide-lifetimes.md index 66faee3fa04c8..545d286c5d71d 100644 --- a/src/doc/guide-lifetimes.md +++ b/src/doc/guide-lifetimes.md @@ -305,10 +305,9 @@ copying. # Circle(Point, f64), // origin, radius # Rectangle(Point, Size) // upper-left, dimensions # } -# static tau: f64 = 6.28; fn compute_area(shape: &Shape) -> f64 { match *shape { - Circle(_, radius) => 0.5 * tau * radius * radius, + Circle(_, radius) => 2.0 * std::f64::consts::PI * radius * radius, Rectangle(_, ref size) => size.w * size.h } } @@ -316,10 +315,7 @@ fn compute_area(shape: &Shape) -> f64 { The first case matches against circles. Here, the pattern extracts the radius from the shape variant and the action uses it to compute the -area of the circle. (Like any up-to-date engineer, we use the [tau -circle constant][tau] and not that dreadfully outdated notion of pi). - -[tau]: http://www.math.utah.edu/~palais/pi.html +area of the circle. The second match is more interesting. Here we match against a rectangle and extract its size: but rather than copy the `size` From ee1cbb9c71bfab8500dfabedb35ba63dd1e5b7ff Mon Sep 17 00:00:00 2001 From: Steve Klabnik Date: Wed, 1 Oct 2014 17:12:29 -0400 Subject: [PATCH 2/5] use similar syntax in all arms Fixes #17672 --- src/doc/guide.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/doc/guide.md b/src/doc/guide.md index 074dfc17b0d78..ac3a58ad39cdb 100644 --- a/src/doc/guide.md +++ b/src/doc/guide.md @@ -2030,7 +2030,7 @@ fn main() { match cmp(input, secret_number) { Less => println!("Too small!"), Greater => println!("Too big!"), - Equal => { println!("You win!"); }, + Equal => println!("You win!"), } } From e2357cf41b69c6db57bbf53c63f59376576c72ae Mon Sep 17 00:00:00 2001 From: Steve Klabnik Date: Wed, 1 Oct 2014 17:14:29 -0400 Subject: [PATCH 3/5] Don't compare () to null. Fixes #17671. --- src/doc/guide.md | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/doc/guide.md b/src/doc/guide.md index ac3a58ad39cdb..30bb48ffccbf3 100644 --- a/src/doc/guide.md +++ b/src/doc/guide.md @@ -659,14 +659,12 @@ error: mismatched types: expected `int` but found `()` (expected int but found ( ``` We expected an integer, but we got `()`. `()` is pronounced 'unit', and is a -special type in Rust's type system. `()` is different than `null` in other -languages, because `()` is distinct from other types. For example, in C, `null` -is a valid value for a variable of type `int`. In Rust, `()` is _not_ a valid -value for a variable of type `int`. It's only a valid value for variables of -the type `()`, which aren't very useful. Remember how we said statements don't -return a value? Well, that's the purpose of unit in this case. The semicolon -turns any expression into a statement by throwing away its value and returning -unit instead. +special type in Rust's type system. In Rust, `()` is _not_ a valid value for a +variable of type `int`. It's only a valid value for variables of the type `()`, +which aren't very useful. Remember how we said statements don't return a value? +Well, that's the purpose of unit in this case. The semicolon turns any +expression into a statement by throwing away its value and returning unit +instead. There's one more time in which you won't see a semicolon at the end of a line of Rust code. For that, we'll need our next concept: functions. From dc35a53d15527e5618d9531b4452bae857f60169 Mon Sep 17 00:00:00 2001 From: Steve Klabnik Date: Wed, 1 Oct 2014 17:16:34 -0400 Subject: [PATCH 4/5] Fix incorrect statement about ok() Fixes #17676. --- src/doc/guide.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/doc/guide.md b/src/doc/guide.md index 30bb48ffccbf3..3ffb7cad0a4cb 100644 --- a/src/doc/guide.md +++ b/src/doc/guide.md @@ -1678,11 +1678,11 @@ just `int`s. Rust provides a method on these `IoResult`s called `ok()`, which does the same thing as our `match` statement, but assuming that we have a valid value. -If we don't, it will terminate our program. In this case, if we can't get -input, our program doesn't work, so we're okay with that. In most cases, we -would want to handle the error case explicitly. The result of `ok()` has a -method, `expect()`, which allows us to give an error message if this crash -happens. +We then call `expect()` on the result, which will terminate our program if we +don't have a valid value. In this case, if we can't get input, our program +doesn't work, so we're okay with that. In most cases, we would want to handle +the error case explicitly. `expect()` allows us to give an error message if +this crash happens. We will cover the exact details of how all of this works later in the Guide. For now, this gives you enough of a basic understanding to work with. From 16cca6dbada93a7468825e0eb106ea05a7417c01 Mon Sep 17 00:00:00 2001 From: Steve Klabnik Date: Thu, 2 Oct 2014 15:01:25 -0400 Subject: [PATCH 5/5] I am bad at math --- src/doc/guide-lifetimes.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/doc/guide-lifetimes.md b/src/doc/guide-lifetimes.md index 545d286c5d71d..dd79d63d51453 100644 --- a/src/doc/guide-lifetimes.md +++ b/src/doc/guide-lifetimes.md @@ -307,7 +307,7 @@ copying. # } fn compute_area(shape: &Shape) -> f64 { match *shape { - Circle(_, radius) => 2.0 * std::f64::consts::PI * radius * radius, + Circle(_, radius) => std::f64::consts::PI * radius * radius, Rectangle(_, ref size) => size.w * size.h } }