Skip to content

Commit fd17f46

Browse files
committed
Guide: Cleanup and proofread
1 parent 6ee56c9 commit fd17f46

File tree

1 file changed

+66
-44
lines changed

1 file changed

+66
-44
lines changed

src/doc/guide.md

Lines changed: 66 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ the documentation for your shell for more details.
133133

134134
Let's make a new source file next. I'm going to use the syntax `editor
135135
filename` to represent editing a file in these examples, but you should use
136-
whatever method you want. We'll call our file `main.rs`:
136+
whatever method you want (if you are new to *NIX try out emacs or vim investing
137+
time in them really is useful). We'll call our file `main.rs`:
137138

138139
```{bash}
139140
$ editor main.rs
@@ -170,11 +171,11 @@ fn main() {
170171
These lines define a **function** in Rust. The `main` function is special:
171172
it's the beginning of every Rust program. The first line says "I'm declaring a
172173
function named `main`, which takes no arguments and returns nothing." If there
173-
were arguments, they would go inside the parentheses (`(` and `)`), and because
174-
we aren't returning anything from this function, we've dropped that notation
175-
entirely. We'll get to it later.
174+
were arguments, they would go inside the parentheses after the function name.
175+
Since we aren't returning anything from this function, we've dropped that notation
176+
entirely. We'll get to it later.
176177

177-
You'll also note that the function is wrapped in curly braces (`{` and `}`).
178+
You'll also note that the function is wrapped in curly braces.
178179
Rust requires these around all function bodies. It is also considered good
179180
style to put the opening curly brace on the same line as the function
180181
declaration, with one space in between.
@@ -192,7 +193,8 @@ with the tab key. We provide some [sample configurations for various
192193
editors](https://github.com/rust-lang/rust/tree/master/src/etc).
193194

194195
The second point is the `println!()` part. This is calling a Rust **macro**,
195-
which is how metaprogramming is done in Rust. If it were a function instead, it
196+
which is how [metaprogramming](http://en.wikipedia.org/wiki/Metaprogramming)
197+
(programs that write programs) is done in Rust. If it were a function instead, it
196198
would look like this: `println()`. For our purposes, we don't need to worry
197199
about this difference. Just know that sometimes, you'll see a `!`, and that
198200
means that you're calling a macro instead of a normal function. Rust implements
@@ -248,7 +250,7 @@ This prints out our `Hello, world!` text to our terminal.
248250
If you come from a dynamically typed language like Ruby, Python, or JavaScript,
249251
you may not be used to these two steps being separate. Rust is an
250252
**ahead-of-time compiled language**, which means that you can compile a
251-
program, give it to someone else, and they don't need to have Rust installed.
253+
program, give it to someone else, and they don't need to have a `Rust` compiler.
252254
If you give someone a `.rb` or `.py` or `.js` file, they need to have
253255
Ruby/Python/JavaScript installed, but you just need one command to both compile
254256
and run your program. Everything is a tradeoff in language design, and Rust has
@@ -438,10 +440,10 @@ something you may not have cared to mutate. If bindings were mutable by
438440
default, the compiler would not be able to tell you this. If you _did_ intend
439441
mutation, then the solution is quite easy: add `mut`.
440442

441-
There are other good reasons to avoid mutable state when possible, but they're
442-
out of the scope of this guide. In general, you can often avoid explicit
443-
mutation, and so it is preferable in Rust. That said, sometimes, mutation is
444-
what you need, so it's not verboten.
443+
There are other good reasons to avoid mutable state when possible (mostly
444+
parallelism, concurrency and code generation) , but we won't go into the details
445+
in this guide. In general, you can often avoid explicit mutation, and so it is
446+
preferable in Rust. That said, sometimes, mutation is what you need, so it's not verboten.
445447

446448
Let's get back to bindings. Rust variable bindings have one more aspect that
447449
differs from other languages: bindings are required to be initialized with a
@@ -660,7 +662,7 @@ Note the semicolons after the 10 and 15. Rust will give us the following error:
660662
error: mismatched types: expected `int` but found `()` (expected int but found ())
661663
```
662664

663-
We expected an integer, but we got `()`. `()` is pronounced 'unit', and is a
665+
We expected an integer, but we got `()`. `()` is pronounced 'unit,' and is a
664666
special type in Rust's type system. In Rust, `()` is _not_ a valid value for a
665667
variable of type `int`. It's only a valid value for variables of the type `()`,
666668
which aren't very useful. Remember how we said statements don't return a value?
@@ -681,8 +683,8 @@ fn main() {
681683
```
682684

683685
This is the simplest possible function declaration. As we mentioned before,
684-
`fn` says 'this is a function,' followed by the name, some parenthesis because
685-
this function takes no arguments, and then some curly braces to indicate the
686+
`fn` says 'this is a function,' followed by the name, nothing in the parentheses
687+
because this function takes no arguments, and then some curly braces to indicate the
686688
body. Here's a function named `foo`:
687689

688690
```{rust}
@@ -710,7 +712,7 @@ fn print_number(x: int) {
710712
}
711713
```
712714

713-
As you can see, function arguments work very similar to `let` declarations:
715+
As you can see, function arguments declarations work very similar to `let` declarations:
714716
you add a type to the argument name, after a colon.
715717

716718
Here's a complete program that adds two numbers together and prints them:
@@ -750,17 +752,27 @@ types explicitly is a best-practice. We agree that forcing functions to declare
750752
types while allowing for inference inside of function bodies is a wonderful
751753
sweet spot between full inference and no inference.
752754

753-
What about returning a value? Here's a function that adds one to an integer:
755+
What about returning a value?
756+
Rust functions return exactly one value, the type of which is declared after an arrow
757+
(`->`) in the function declaration. Here is the declaration of a function that
758+
returns an `int`:
759+
760+
```{rust, ignore}
761+
fn returns_int() -> int {
762+
//...
763+
}
764+
```
765+
766+
Rust function usually return their last expression as return value.
767+
Here's a function that adds one to an integer:
754768

755769
```{rust}
756770
fn add_one(x: int) -> int {
757771
x + 1
758772
}
759773
```
760774

761-
Rust functions return exactly one value, and you declare the type after an
762-
'arrow', which is a dash (`-`) followed by a greater-than sign (`>`).
763-
775+
As you can see the last line evaluates to an `int`, which is the return type of the function.
764776
You'll note the lack of a semicolon here. If we added it in:
765777

766778
```{ignore}
@@ -822,8 +834,8 @@ that we haven't learned about yet, so let's just leave it at that for now.
822834
# Comments
823835

824836
Now that we have some functions, it's a good idea to learn about comments.
825-
Comments are notes that you leave to other programmers to help explain things
826-
about your code. The compiler mostly ignores them.
837+
Comments are notes that you leave to other programmers (and your future self)
838+
to help explain things about your code. The compiler mostly ignores them.
827839

828840
Rust has two kinds of comments that you should care about: **line comment**s
829841
and **doc comment**s.
@@ -882,7 +894,7 @@ Tuples are an ordered list of a fixed size. Like this:
882894
let x = (1i, "hello");
883895
```
884896

885-
The parenthesis and commas form this two-length tuple. Here's the same code, but
897+
The parentheses and commas form this two-length tuple. Here's the same code, but
886898
with the type annotated:
887899

888900
```rust
@@ -1066,7 +1078,7 @@ destructuring `let`.
10661078

10671079
## Enums
10681080

1069-
Finally, Rust has a "sum type", an **enum**. Enums are an incredibly useful
1081+
Finally, Rust has a 'sum type,' an **enum**. Enums are an incredibly useful
10701082
feature of Rust, and are used throughout the standard library. This is an enum
10711083
that is provided by the Rust standard library:
10721084

@@ -1283,6 +1295,8 @@ It can also allow us to treat errors or unexpected computations, for example, a
12831295
function that is not guaranteed to be able to compute a result (an `int` here),
12841296
could return an `OptionalInt`, and we would handle that value with a `match`.
12851297
As you can see, `enum` and `match` used together are quite useful!
1298+
They are also the only way to get the values contained in an
1299+
`enum`.
12861300

12871301
`match` is also an expression, which means we can use it on the right
12881302
hand side of a `let` binding or directly where an expression is
@@ -1542,8 +1556,8 @@ always initialized.
15421556
let a = [0i, ..20]; // Shorthand for array of 20 elements all initialized to 0
15431557
```
15441558

1545-
Arrays have type `[T,..N]`. We'll talk about this `T` notation later, when we
1546-
cover generics.
1559+
An array of `N` elements of type `T` has type `[T,..N]`. We'll talk about this `T`
1560+
notation later, when we cover generics.
15471561

15481562
You can get the number of elements in an array `a` with `a.len()`, and use
15491563
`a.iter()` to iterate over them with a for loop. This code will print each
@@ -1591,6 +1605,7 @@ arrays. In addition, (mutable) vectors can grow automatically:
15911605

15921606
```{rust}
15931607
let mut nums = vec![1i, 2, 3];
1608+
println!("The length of nums is {}", nums.len()); // Prints 3
15941609
nums.push(4);
15951610
println!("The length of nums is now {}", nums.len()); // Prints 4
15961611
```
@@ -1645,7 +1660,7 @@ std::io::stdin();
16451660
```
16461661

16471662
This calls a function, `stdin()`, that lives inside the `std::io` module. As
1648-
you can imagine, everything in `std` is provided by Rust, the 'standard
1663+
you can imagine, everything in `std` is provided by Rust's 'standard
16491664
library.' We'll talk more about the module system later.
16501665

16511666
Since writing the fully qualified name all the time is annoying, we can use
@@ -1739,7 +1754,7 @@ doesn't work, so we're okay with that. In most cases, we would want to handle
17391754
the error case explicitly. `expect()` allows us to give an error message if
17401755
this crash happens.
17411756

1742-
We will cover the exact details of how all of this works later in the Guide.
1757+
We will cover the exact details of how all of this works in another Guide.
17431758
For now, this gives you enough of a basic understanding to work with.
17441759

17451760
Back to the code we were working on! Here's a refresher:
@@ -1931,7 +1946,7 @@ explained. We then added in a `let` expression to create a variable binding
19311946
named `secret_number`, and we printed out its result.
19321947

19331948
Also, you may wonder why we are using `%` on the result of `rand::random()`.
1934-
This operator is called 'modulo', and it returns the remainder of a division.
1949+
This operator is called 'modulo,' and it returns the remainder of a division.
19351950
By taking the modulo of the result of `rand::random()`, we're limiting the
19361951
values to be between 0 and 99. Then, we add one to the result, making it from 1
19371952
to 100. Using modulo can give you a very, very small bias in the result, but
@@ -2778,7 +2793,7 @@ mod hello {
27782793
}
27792794
```
27802795

2781-
Usage of the `pub` keyword is sometimes called 'exporting', because
2796+
Usage of the `pub` keyword is sometimes called 'exporting,' because
27822797
we're making the function available for other modules. This will work:
27832798

27842799
```{notrust,ignore}
@@ -2791,6 +2806,8 @@ Hello, world!
27912806
Nice! There are more things we can do with modules, including moving them into
27922807
their own files. This is enough detail for now.
27932808

2809+
More can be found in the [Crates and Modules Guide](http://doc.rust-lang.org/guide-crates.html).
2810+
27942811
# Testing
27952812

27962813
Traditionally, testing has not been a strong suit of most systems programming
@@ -3524,7 +3541,7 @@ difficult.
35243541
Rust chooses a different path, and that path is called **ownership**. Any
35253542
binding that creates a resource is the **owner** of that resource.
35263543

3527-
Being an owner affords you some privileges:
3544+
Being an owner grants you some privileges:
35283545

35293546
1. You control when that resource is deallocated.
35303547
2. You may lend that resource, immutably, to as many borrowers as you'd like.
@@ -4111,14 +4128,20 @@ binding name and two parentheses, just like we would for a named function.
41114128
Let's compare syntax. The two are pretty close:
41124129

41134130
```{rust}
4114-
let add_one = |x: int| -> int { 1i + x };
4131+
let add_one = |x| { 1i + x };
41154132
fn add_one (x: int) -> int { 1i + x }
41164133
```
41174134

41184135
As you may have noticed, closures infer their argument and return types, so you
41194136
don't need to declare one. This is different from named functions, which
41204137
default to returning unit (`()`).
41214138

4139+
Nevertheless, we can still declare argument and return types as follows:
4140+
4141+
```{rust}
4142+
let add_one = |x: int| -> int { 1i + x };
4143+
```
4144+
41224145
There's one big difference between a closure and named functions, and it's in
41234146
the name: a closure "closes over its environment." What does that mean? It means
41244147
this:
@@ -4190,7 +4213,7 @@ fn twice(x: int, f: |int| -> int) -> int {
41904213
}
41914214
41924215
fn main() {
4193-
let square = |x: int| { x * x };
4216+
let square = |x: int| -> int { x * x };
41944217
41954218
twice(5i, square); // evaluates to 50
41964219
}
@@ -4199,7 +4222,7 @@ fn main() {
41994222
Let's break the example down, starting with `main`:
42004223

42014224
```{rust}
4202-
let square = |x: int| { x * x };
4225+
let square = |x: int| -> int { x * x };
42034226
```
42044227

42054228
We've seen this before. We make a closure that takes an integer, and returns
@@ -4266,7 +4289,7 @@ fn twice(x: int, f: |int| -> int) -> int {
42664289
}
42674290
42684291
fn main() {
4269-
twice(5i, |x: int| { x * x }); // evaluates to 50
4292+
twice(5i, |x: int| -> int { x * x }); // evaluates to 50
42704293
}
42714294
```
42724295

@@ -4450,7 +4473,7 @@ Another important consumer is `fold`. Here's what it looks like:
44504473

44514474
```{rust}
44524475
let sum = range(1i, 4i)
4453-
.fold(0i, |sum, x| sum + x);
4476+
.fold(0i, |acc, x| acc + x);
44544477
```
44554478

44564479
`fold()` is a consumer that looks like this:
@@ -4518,8 +4541,8 @@ Now, `collect()` will require that `range()` give it some numbers, and so
45184541
it will do the work of generating the sequence.
45194542

45204543
`range` is one of two basic iterators that you'll see. The other is `iter()`,
4521-
which you've used before. `iter()` can turn a vector into a simple iterator
4522-
that gives you each element in turn:
4544+
which you've used before. `iter()` can turn an iterable structure, such as a
4545+
vector or a list, into a simple iterator that gives you each element in turn:
45234546

45244547
```{rust}
45254548
let nums = [1i, 2i, 3i];
@@ -4722,9 +4745,9 @@ enum Result<H, N> {
47224745
if we wanted to. Convention says that the first generic parameter should be
47234746
`T`, for 'type,' and that we use `E` for 'error.' Rust doesn't care, however.
47244747

4725-
The `Result<T, E>` type is intended to
4726-
be used to return the result of a computation, and to have the ability to
4727-
return an error if it didn't work out. Here's an example:
4748+
The `Result<T, E>` type is intended to be used to return the result of a computation,
4749+
and to have the ability to return an error if it didn't work out, just as we saw
4750+
in the enum chapter. Here's an example:
47284751

47294752
```{rust}
47304753
let x: Result<f64, String> = Ok(2.3f64);
@@ -5247,7 +5270,7 @@ immediately.
52475270

52485271
## Success and failure
52495272

5250-
Tasks don't always succeed, they can also panic. A task that wishes to panic
5273+
Tasks don't always succeed, they can also panic. A task that wishes to panic
52515274
can call the `panic!` macro, passing a message:
52525275

52535276
```{rust}
@@ -5284,7 +5307,7 @@ allow you to provide abstractions over syntax. Do you wish Rust had the ability
52845307
to do something that it can't currently do? You may be able to write a macro
52855308
to extend Rust's capabilities.
52865309

5287-
You've already used one macro extensively: `println!`. When we invoke
5310+
You've already used two macros extensively: `println!` and `vec!`. When we invoke
52885311
a Rust macro, we need to use the exclamation mark (`!`). There are two reasons
52895312
why this is so: the first is that it makes it clear when you're using a
52905313
macro. The second is that macros allow for flexible syntax, and so Rust must
@@ -5364,8 +5387,7 @@ this by hand to get a type-checked `println`.
53645387

53655388
For more on macros, please consult [the Macros Guide](guide-macros.html).
53665389
Macros are a very advanced and still slightly experimental feature, but they don't
5367-
require a deep understanding to be called, since they look just like functions. The
5368-
Guide can help you if you want to write your own.
5390+
require a deep understanding to be called, since they look just like functions.
53695391

53705392
# Unsafe
53715393

0 commit comments

Comments
 (0)