]> git.lizzy.rs Git - rust.git/blobdiff - src/doc/guide.md
rollup merge of #17355 : gamazeps/issue17210
[rust.git] / src / doc / guide.md
index fc8279a778efbb08e49935989a8937711f3ff54d..03a73db8aa48de541a55dcf4e342a16a69bac6b5 100644 (file)
@@ -1,14 +1,5 @@
 % The Rust Guide
 
-<div style="border: 2px solid red; padding:5px;">
-This guide is a work in progress. Until it is ready, we highly recommend that
-you read the <a href="tutorial.html">Tutorial</a> instead. This work-in-progress Guide is being
-displayed here in line with Rust's open development policy. Please open any
-issues you find as usual.
-</div>
-
-# Welcome!
-
 Hey there! Welcome to the Rust guide. This is the place to be if you'd like to
 learn how to program in Rust. Rust is a systems programming language with a
 focus on "high-level, bare-metal programming": the lowest level control a
@@ -38,8 +29,11 @@ $ curl -s https://static.rust-lang.org/rustup.sh | sudo sh
 (If you're concerned about `curl | sudo sh`, please keep reading. Disclaimer
 below.)
 
-If you're on Windows, please [download this .exe and run
-it](https://static.rust-lang.org/dist/rust-nightly-install.exe).
+If you're on Windows, please download either the [32-bit
+installer](https://static.rust-lang.org/dist/rust-nightly-i686-w64-mingw32.exe)
+or the [64-bit
+installer](https://static.rust-lang.org/dist/rust-nightly-x86_64-w64-mingw32.exe)
+and run it.
 
 If you decide you don't want Rust anymore, we'll be a bit sad, but that's okay.
 Not every programming language is great for everyone. Just pass an argument to
@@ -119,7 +113,7 @@ The first thing that we need to do is make a file to put our code in. I like
 to make a `projects` directory in my home directory, and keep all my projects
 there. Rust does not care where your code lives.
 
-This actually leads to one other concern we should address: this tutorial will
+This actually leads to one other concern we should address: this guide will
 assume that you have basic familiarity with the command line. Rust does not
 require that you know a whole ton about the command line, but until the
 language is in a more finished state, IDE support is spotty. Rust makes no
@@ -151,7 +145,7 @@ in your file name, use an underscore. `hello_world.rs` rather than
 
 Now that you've got your file open, type this in:
 
-```
+```{rust}
 fn main() {
     println!("Hello, world!");
 }
@@ -161,19 +155,19 @@ Save the file, and then type this into your terminal window:
 
 ```{bash}
 $ rustc main.rs
-$ ./hello_world # or hello_world.exe on Windows
+$ ./main # or main.exe on Windows
 Hello, world!
 ```
 
 Success! Let's go over what just happened in detail.
 
-```
+```{rust}
 fn main() {
 
 }
 ```
 
-These two lines define a **function** in Rust. The `main` function is special:
+These lines define a **function** in Rust. The `main` function is special:
 it's the beginning of every Rust program. The first line says "I'm declaring a
 function named `main`, which takes no arguments and returns nothing." If there
 were arguments, they would go inside the parentheses (`(` and `)`), and because
@@ -187,15 +181,15 @@ declaration, with one space in between.
 
 Next up is this line:
 
-```
+```{rust}
     println!("Hello, world!");
 ```
 
 This line does all of the work in our little program. There are a number of
 details that are important here. The first is that it's indented with four
 spaces, not tabs. Please configure your editor of choice to insert four spaces
-with the tab key. We provide some sample configurations for various editors
-[here](https://github.com/rust-lang/rust/tree/master/src/etc).
+with the tab key. We provide some [sample configurations for various
+editors](https://github.com/rust-lang/rust/tree/master/src/etc).
 
 The second point is the `println!()` part. This is calling a Rust **macro**,
 which is how metaprogramming is done in Rust. If it were a function instead, it
@@ -216,7 +210,7 @@ Finally, the line ends with a semicolon (`;`). Rust is an **expression
 oriented** language, which means that most things are expressions. The `;` is
 used to indicate that this expression is over, and the next one is ready to
 begin. Most lines of Rust code end with a `;`. We will cover this in-depth
-later in the tutorial.
+later in the guide.
 
 Finally, actually **compiling** and **running** our program. We can compile
 with our compiler, `rustc`, by passing it the name of our source file:
@@ -241,10 +235,10 @@ main.exe  main.rs
 ```
 
 There are now two files: our source code, with the `.rs` extension, and the
-executable (`hello_world.exe` on Windows, `hello_world` everywhere else)
+executable (`main.exe` on Windows, `main` everywhere else)
 
 ```{bash}
-$ ./hello_world  # or hello_world.exe on Windows
+$ ./main  # or main.exe on Windows
 ```
 
 This prints out our `Hello, world!` text to our terminal.
@@ -401,14 +395,10 @@ By the way, in these examples, `i` indicates that the number is an integer.
 
 Rust is a statically typed language, which means that we specify our types up
 front. So why does our first example compile? Well, Rust has this thing called
-"[Hindley-Milner type
-inference](http://en.wikipedia.org/wiki/Hindley%E2%80%93Milner_type_system)",
-named after some really smart type theorists. If you clicked that link, don't
-be scared: what this means for you is that Rust will attempt to infer the types
-in your program, and it's pretty good at it. If it can infer the type, Rust
+"type inference." If it can figure out what the type of something is, Rust
 doesn't require you to actually type it out.
 
-We can add the type if we want to. Types come after a colon (`:`):
+We can add the type if we want to, though. Types come after a colon (`:`):
 
 ```{rust}
 let x: int = 5;
@@ -563,7 +553,7 @@ the block is executed. If it's `false`, then it is not.
 
 If you want something to happen in the `false` case, use an `else`:
 
-```
+```{rust}
 let x = 5i;
 
 if x == 5i {
@@ -575,7 +565,8 @@ if x == 5i {
 
 This is all pretty standard. However, you can also do this:
 
-```
+
+```{rust}
 let x = 5i;
 
 let y = if x == 5i {
@@ -587,7 +578,7 @@ let y = if x == 5i {
 
 Which we can (and probably should) write like this:
 
-```
+```{rust}
 let x = 5i;
 
 let y = if x == 5i { 10i } else { 15i };
@@ -644,7 +635,7 @@ every line of Rust code you see.
 What is this exception that makes us say 'almost?' You saw it already, in this
 code:
 
-```
+```{rust}
 let x = 5i;
 
 let y: int = if x == 5i { 10i } else { 15i };
@@ -990,7 +981,7 @@ notation: `origin.x`.
 The values in structs are immutable, like other bindings in Rust. However, you
 can use `mut` to make them mutable:
 
-```rust
+```{rust}
 struct Point {
     x: int,
     y: int,
@@ -1014,7 +1005,7 @@ called a **tuple struct**. Tuple structs do have a name, but their fields
 don't:
 
 
-```
+```{rust}
 struct Color(int, int, int);
 struct Point(int, int, int);
 ```
@@ -1029,7 +1020,7 @@ let origin = Point(0, 0, 0);
 It is almost always better to use a struct than a tuple struct. We would write
 `Color` and `Point` like this instead:
 
-```rust
+```{rust}
 struct Color {
     red: int,
     blue: int,
@@ -1050,7 +1041,7 @@ There _is_ one case when a tuple struct is very useful, though, and that's a
 tuple struct with only one element. We call this a 'newtype,' because it lets
 you create a new type that's a synonym for another one:
 
-```
+```{rust}
 struct Inches(int);
 
 let length = Inches(10);
@@ -1167,7 +1158,7 @@ what's the solution?
 Rust has a keyword, `match`, that allows you to replace complicated `if`/`else`
 groupings with something more powerful. Check it out:
 
-```rust
+```{rust}
 let x = 5i;
 
 match x {
@@ -1289,15 +1280,15 @@ two main looping constructs: `for` and `while`.
 
 The `for` loop is used to loop a particular number of times. Rust's `for` loops
 work a bit differently than in other systems languages, however. Rust's `for`
-loop doesn't look like this C `for` loop:
+loop doesn't look like this "C style" `for` loop:
 
-```{ignore,c}
+```{c}
 for (x = 0; x < 10; x++) {
     printf( "%d\n", x );
 }
 ```
 
-It looks like this:
+Instead, it looks like this:
 
 ```{rust}
 for x in range(0i, 10i) {
@@ -1320,14 +1311,13 @@ valid for the loop body. Once the body is over, the next value is fetched from
 the iterator, and we loop another time. When there are no more values, the
 `for` loop is over.
 
-In our example, the `range` function is a function, provided by Rust, that
-takes a start and an end position, and gives an iterator over those values. The
-upper bound is exclusive, though, so our loop will print `0` through `9`, not
-`10`.
+In our example, `range` is a function that takes a start and an end position,
+and gives an iterator over those values. The upper bound is exclusive, though,
+so our loop will print `0` through `9`, not `10`.
 
 Rust does not have the "C style" `for` loop on purpose. Manually controlling
 each element of the loop is complicated and error prone, even for experienced C
-developers. 
+developers.
 
 We'll talk more about `for` when we cover **iterator**s, later in the Guide.
 
@@ -1405,7 +1395,7 @@ We now loop forever with `loop`, and use `break` to break out early.
 `continue` is similar, but instead of ending the loop, goes to the next
 iteration: This will only print the odd numbers:
 
-```
+```{rust}
 for x in range(0i, 10i) {
     if x % 2 == 0 { continue; }
 
@@ -1585,8 +1575,6 @@ we haven't seen before. Here's a simple program that reads some input,
 and then prints it back out:
 
 ```{rust,ignore}
-use std::io;
-
 fn main() {
     println!("Type something!");
 
@@ -2014,7 +2002,7 @@ Great! Next up: let's compare our guess to the secret guess.
 
 ## Comparing guesses
 
-If you remember, earlier in the tutorial, we made a `cmp` function that compared
+If you remember, earlier in the guide, we made a `cmp` function that compared
 two numbers. Let's add that in, along with a `match` statement to compare the
 guess to the secret guess:
 
@@ -3931,7 +3919,7 @@ the ability to use this **method call syntax** via the `impl` keyword.
 
 Here's how it works:
 
-```
+```{rust}
 struct Circle {
     x: f64,
     y: f64,
@@ -3970,7 +3958,7 @@ multiplications later, and we have our area.
 You can also define methods that do not take a `self` parameter. Here's a
 pattern that's very common in Rust code:
 
-```
+```{rust}
 struct Circle {
     x: f64,
     y: f64,
@@ -4281,7 +4269,7 @@ very common with iterators: we can ignore unnecessary bounds checks, but still
 know that we're safe.
 
 There's another detail here that's not 100% clear because of how `println!`
-works. `num` is actually of type `&int`, that is, it's a reference to an `int`,
+works. `num` is actually of type `&int`. That is, it's a reference to an `int`,
 not an `int` itself. `println!` handles the dereferencing for us, so we don't
 see it. This code works fine too: