Auto merge of #28930 - steveklabnik:update_pr, r=steveklabnik

https://github.com/rust-lang/rust/pull/27813#issuecomment-146842041
This commit is contained in:
bors 2015-10-10 07:56:49 +00:00
commit 8091cb139f
5 changed files with 157 additions and 153 deletions

View File

@ -9,9 +9,8 @@ requirements, and writing low-level code, like device drivers and operating
systems. It improves on current languages targeting this space by having a
number of compile-time safety checks that produce no runtime overhead, while
eliminating all data races. Rust also aims to achieve zero-cost abstractions
even though some of these abstractions feel like those of a high-level
language. Even then, Rust still allows precise control like a low-level
language would.
even though some of these abstractions feel like those of a high-level language.
Even then, Rust still allows precise control like a low-level language would.
[rust]: https://www.rust-lang.org
@ -34,10 +33,10 @@ is the first. After this:
[gl]: glossary.html
[bi]: bibliography.html
After reading this introduction, youll want to dive into either Learn Rust
or Syntax and Semantics, depending on your preference: Learn Rust if you
want to dive in with a project, or Syntax and Semantics if you prefer to
start small, and learn a single concept thoroughly before moving onto the next.
After reading this introduction, youll want to dive into either Learn Rust or
Syntax and Semantics, depending on your preference: Learn Rust if you want
to dive in with a project, or Syntax and Semantics if you prefer to start
small, and learn a single concept thoroughly before moving onto the next.
Copious cross-linking connects these parts together.
### Contributing
@ -76,11 +75,11 @@ type inference to balance out the power of static typing with the verbosity of
annotating types.
Rust prefers stack allocation to heap allocation: `x` is placed directly on the
stack. However, the `Vec<T>` type allocates space for the elements of the
vector on the heap. If youre not familiar with this distinction, you can
ignore it for now, or check out [The Stack and the Heap][heap]. As a systems
programming language, Rust gives you the ability to control how your memory is
allocated, but when were getting started, its less of a big deal.
stack. However, the `Vec<T>` type allocates space for the elements of the vector
on the heap. If youre not familiar with this distinction, you can ignore it for
now, or check out [The Stack and the Heap][heap]. As a systems programming
language, Rust gives us the ability to control how our memory is allocated, but
when were getting started, its less of a big deal.
[var]: variable-bindings.html
[macro]: macros.html
@ -90,10 +89,10 @@ Earlier, we mentioned that ownership is the key new concept in Rust. In Ru
parlance, `x` is said to own the vector. This means that when `x` goes out of
scope, the vectors memory will be de-allocated. This is done deterministically
by the Rust compiler, rather than through a mechanism such as a garbage
collector. In other words, in Rust, you dont call functions like `malloc` and
`free` yourself: the compiler statically determines when you need to allocate
or deallocate memory, and inserts those calls itself. To err is to be human,
but compilers never forget.
collector. In other words, in Rust, we dont call functions like `malloc` and
`free` ourselves: the compiler statically determines when we need to allocate or
deallocate memory, and inserts those calls itself. To err is to be human, but
compilers never forget.
Lets add another line to our example:
@ -105,13 +104,13 @@ fn main() {
}
```
Weve introduced another binding, `y`. In this case, `y` is a reference to
the first element of the vector. Rusts references are similar to pointers in
other languages, but with additional compile-time safety checks. References
interact with the ownership system by [borrowing][borrowing] what they point
to, rather than owning it. The difference is, when the reference goes out of
scope, it will not deallocate the underlying memory. If it did, wed
de-allocate twice, which is bad!
Weve introduced another binding, `y`. In this case, `y` is a reference to the
first element of the vector. Rusts references are similar to pointers in other
languages, but with additional compile-time safety checks. References interact
with the ownership system by [borrowing][borrowing] what they point to, rather
than owning it. The difference is, when the reference goes out of scope, it
won't deallocate the underlying memory. If it did, wed de-allocate twice, which
is bad!
[borrowing]: references-and-borrowing.html
@ -147,7 +146,7 @@ fn main() {
Whew! The Rust compiler gives quite detailed errors at times, and this is one
of those times. As the error explains, while we made our binding mutable, we
still cannot call `push`. This is because we already have a reference to an
still can't call `push`. This is because we already have a reference to an
element of the vector, `y`. Mutating something while another reference exists
is dangerous, because we may invalidate the reference. In this specific case,
when we create the vector, we may have only allocated space for two elements.

View File

@ -1,5 +1,5 @@
% Getting Started
This first section of the book will get you going with Rust and its tooling.
This first section of the book will get us going with Rust and its tooling.
First, well install Rust. Then, the classic Hello World program. Finally,
well talk about Cargo, Rusts build system and package manager.

View File

@ -7,15 +7,16 @@ so it is assumed that Rust projects will use Cargo from the beginning.
[cratesio]: http://doc.crates.io
Cargo manages three things: building your code, downloading the dependencies
your code needs, and building those dependencies. At first, your program doesnt
have any dependencies, so well only be using the first part of its
functionality. Eventually, well add more. Since we started off by using Cargo,
it'll be easy to add later.
Cargo manages three things: building our code, downloading the dependencies our
code needs, and building those dependencies. At first, our program doesnt have
any dependencies, so well only be using the first part of its functionality.
Eventually, well add more. Since we started off by using Cargo, it'll be easy
to add later.
If we installed Rust via the official installers we will also have Cargo. If we
installed Rust some other way, we may want to [check the Cargo
README][cargoreadme] for specific instructions about installing it.
If you installed Rust via the official installers you will also have Cargo. If
you installed Rust some other way, you may want to
[check the Cargo README][cargoreadme] for specific instructions about installing
it.
[cargoreadme]: https://github.com/rust-lang/cargo#installing-cargo-from-nightlies
@ -30,29 +31,29 @@ old executable (`main.exe` on Windows, `main` everywhere else). Let's do that pa
```bash
$ mkdir src
$ mv main.rs src/main.rs
$ rm main # or main.exe on Windows
$ rm main # or 'rm main.exe' on Windows
```
Note that since we're creating an executable, we retain `main.rs` as the source
filename. If we want to make a library instead, we should use `lib.rs`. This
convention is used by Cargo to successfully compile our projects, but it can be
overridden if we wish. Custom file locations for the entry point can be
specified with a [`[lib]` or `[[bin]]`][crates-custom] key in the TOML file.
> Note: since we're creating an executable, we retain `main.rs` as the source
> filename. If we want to make a library instead, we should use `lib.rs`. This
> convention is used by Cargo to successfully compile our projects, but it can
> be overridden if we wish. Custom file locations for the entry point can be
> specified with a [`[lib]` or `[[bin]]`][crates-custom] key in the TOML file.
[crates-custom]: http://doc.crates.io/manifest.html#configuring-a-target
Cargo expects your source files to live inside a `src` directory. That leaves
the top level for other things, like READMEs, license information, and anything
not related to your code. Cargo helps us keep our projects nice and tidy. A
place for everything, and everything in its place.
Cargo expects our source files to live inside a `src` directory. That leaves the
top level for other things, like READMEs, license information, and anything not
related to our code. Cargo helps us keep our projects nice and tidy. A place for
everything, and everything in its place.
Next, our configuration file:
```bash
$ editor Cargo.toml
$ editor Cargo.toml # or 'notepad Cargo.toml' on Windows
```
Make sure to get this name right: you need the capital `C`!
Make sure to get this name right: we need the capital `C`!
Put this inside:
@ -109,8 +110,8 @@ about the future: when our project gets more complex, we need to do more
things to get all of the parts to properly compile. With Cargo, as our project
grows, we can just run `cargo build`, and itll work the right way.
When your project is finally ready for release, you can use
`cargo build --release` to compile your project with optimizations.
When our project is finally ready for release, we can use `cargo build
--release` to compile our project with optimizations.
You'll also notice that Cargo has created a new file: `Cargo.lock`.
@ -120,14 +121,14 @@ name = "hello_world"
version = "0.0.1"
```
The `Cargo.lock` file is used by Cargo to keep track of dependencies in your application.
Right now, we dont have any, so its a bit sparse. You won't ever need
to touch this file yourself, just let Cargo handle it.
The `Cargo.lock` file is used by Cargo to keep track of dependencies in our
application. Right now, we dont have any, so its a bit sparse. We won't ever
need to touch this file ourselves, just let Cargo handle it.
Thats it! Weve successfully built `hello_world` with Cargo. Even though our
program is simple, its using much of the real tooling that youll use for the
rest of your Rust career. You can expect to do this to get started with
virtually all Rust projects:
program is simple, its using much of the real tooling that well use for the
rest of our Rust career. We can expect to do this to get started with virtually
all Rust projects:
```bash
$ git clone someurl.com/foo
@ -137,17 +138,19 @@ $ cargo build
## A New Project
You dont have to go through this whole process every time you want to start a
new project! Cargo has the ability to make a bare-bones project directory in
which you can start developing right away.
We dont have to go through this whole process every time we want to start a new
project! Cargo has the ability to make a bare-bones project directory in which
we can start developing right away.
To start a new project with Cargo, use `cargo new`:
To start a new project with Cargo, we use `cargo new`:
```bash
$ cargo new hello_world --bin
```
Were passing `--bin` because our goal is to get straight to making an executable application, as opposed to a library. Executables are often called binaries. (as in `/usr/bin`, if youre on a Unix system)
Were passing `--bin` because our goal is to get straight to making an
executable application, as opposed to a library. Executables are often called
binaries. (as in `/usr/bin`, if were on a Unix system)
Let's check out what Cargo has generated for us:
@ -162,7 +165,7 @@ $ tree .
1 directory, 2 files
```
If you don't have the `tree` command, you can probably get it from your
If we don't have the `tree` command, we can probably get it from our
distributions package manager. Its not necessary, but its certainly useful.
This is all we need to get started. First, lets check out `Cargo.toml`:
@ -176,7 +179,7 @@ authors = ["Your Name <you@example.com>"]
```
Cargo has populated this file with reasonable defaults based off the arguments
you gave it and your `git` global configuration. You may notice that Cargo has
we gave it and our `git` global configuration. You may notice that Cargo has
also initialized the `hello_world` directory as a `git` repository.
Heres whats in `src/main.rs`:
@ -187,20 +190,21 @@ fn main() {
}
```
Cargo has generated a "Hello World!" for us, and youre ready to start coding! Cargo
has its own [guide][guide] which covers Cargos features in much more depth.
Cargo has generated a "Hello World!" for us, and were ready to start coding!
Cargo has its own [guide][guide] which covers Cargos features in much more
depth.
[guide]: http://doc.crates.io/guide.html
Now that youve got the tools down, lets actually learn more about the Rust
language itself. These are the basics that will serve you well through the rest
of your time with Rust.
Now that weve got the tools down, lets actually learn more about the Rust
language itself. These are the basics that will serve us well through the rest
of our time with Rust.
You have two options: Dive into a project with [Learn Rust][learnrust], or
start from the bottom and work your way up with [Syntax and
Semantics][syntax]. More experienced systems programmers will probably prefer
Learn Rust, while those from dynamic backgrounds may enjoy either. Different
people learn differently! Choose whatevers right for you.
start from the bottom and work your way up with
[Syntax and Semantics][syntax]. More experienced systems programmers will
probably prefer Learn Rust, while those from dynamic backgrounds may enjoy
either. Different people learn differently! Choose whatevers right for you.
[learnrust]: learn-rust.html
[syntax]: syntax-and-semantics.html

View File

@ -1,25 +1,25 @@
% Hello, world!
Now that you have Rust installed, lets write your first Rust program. Its
traditional to make your first program in any new language one that prints the
Now that we have Rust installed, lets write our first Rust program. Its
traditional to make our first program in any new language one that prints the
text “Hello, world!” to the screen. The nice thing about starting with such a
simple program is that you can verify that your compiler isnt just installed,
but also working properly. And printing information to the screen is a pretty
common thing to do.
simple program is that we can verify that our compiler isnt just installed, but
also working properly. And printing information to the screen is a pretty common
thing to do.
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.
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 doesn't care where our code lives.
This actually leads to one other concern we should address: this guide will
assume that you have basic familiarity with the command line. Rust itself makes
no specific demands on your editing tooling, or where your code lives. If you
prefer an IDE to the command line, you may want to check out
[SolidOak][solidoak], or wherever plugins are for your favorite IDE. There are
a number of extensions of varying quality in development by the community. The
Rust team also ships [plugins for various editors][plugins]. Configuring your
assume that we have basic familiarity with the command line. Rust itself makes
no specific demands on our editing tooling, or where our code lives. If we
prefer an IDE to the command line, we may want to check out
[SolidOak][solidoak], or wherever plugins are for our favorite IDE. There are a
number of extensions of varying quality in development by the community. The
Rust team also ships [plugins for various editors][plugins]. Configuring our
editor or IDE is out of the scope of this tutorial, so check the documentation
for your setup, specifically.
for our setup, specifically.
[solidoak]: https://github.com/oakes/SolidOak
[plugins]: https://github.com/rust-lang/rust/blob/master/src/etc/CONFIGS.md
@ -33,14 +33,14 @@ $ mkdir hello_world
$ cd hello_world
```
If youre on Windows and not using PowerShell, the `~` may not work. Consult
the documentation for your shell for more details.
If were on Windows and not using PowerShell, the `~` may not work. Consult the
documentation for our shell for more details.
Lets make a new source file next. Well call our file `main.rs`. Rust files
always end in a `.rs` extension. If youre using more than one word in your
always end in a `.rs` extension. If were using more than one word in our
filename, use an underscore: `hello_world.rs` rather than `helloworld.rs`.
Now that youve got your file open, type this in:
Now that weve got our file open, type this in:
```rust
fn main() {
@ -48,7 +48,7 @@ fn main() {
}
```
Save the file, and then type this into your terminal window:
Save the file, and then type this into our terminal window:
```bash
$ rustc main.rs
@ -72,9 +72,9 @@ we arent returning anything from this function, we can omit the return type
entirely. Well get to it later.
Youll also note that the function is wrapped in curly braces (`{` and `}`).
Rust requires these around all function bodies. It is also considered good
style to put the opening curly brace on the same line as the function
declaration, with one space in between.
Rust requires these around all function bodies. It is also considered good style
to put the opening curly brace on the same line as the function declaration,
with one space in between.
Next up is this line:
@ -85,29 +85,30 @@ Next up is this line:
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 its 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][configs].
with the tab key. We provide some
[sample configurations for various editors][configs].
[configs]: https://github.com/rust-lang/rust/tree/master/src/etc/CONFIGS.md
The second point is the `println!()` part. This is calling a Rust [macro][macro],
which is how metaprogramming is done in Rust. If it were a function instead, it
would look like this: `println()`. For our purposes, we dont need to worry
about this difference. Just know that sometimes, youll see a `!`, and that
means that youre calling a macro instead of a normal function. Rust implements
`println!` as a macro rather than a function for good reasons, but that's an
advanced topic. One last thing to mention: Rusts macros are significantly
different from C macros, if youve used those. Dont be scared of using macros.
Well get to the details eventually, youll just have to trust us for now.
The second point is the `println!()` part. This is calling a Rust
[macro][macro], which is how metaprogramming is done in Rust. If it were a
function instead, it would look like this: `println()`. For our purposes, we
dont need to worry about this difference. Just know that sometimes, well see a
`!`, and that means that were calling a macro instead of a normal function.
Rust implements `println!` as a macro rather than a function for good reasons,
but that's an advanced topic. One last thing to mention: Rusts macros are
significantly different from C macros, if youve used those. Dont be scared of
using macros. Well get to the details eventually, youll just have to take it
on trust for now.
[macro]: macros.html
Next, `"Hello, world!"` is a string. Strings are a surprisingly complicated
topic in a systems programming language, and this is a statically allocated
string. If you want to read further about allocation, check out
[the stack and the heap][allocation], but you dont need to right now if you
dont want to. We pass this string as an argument to `println!`, which prints the
string to the screen. Easy enough!
string. If you want to read further about allocation, check out [the stack and
the heap][allocation], but you dont need to right now if you dont want to. We
pass this string as an argument to `println!`, which prints the string to the
screen. Easy enough!
[allocation]: the-stack-and-the-heap.html
@ -126,8 +127,8 @@ compiler, `rustc`, by passing it the name of our source file:
$ rustc main.rs
```
This is similar to `gcc` or `clang`, if you come from a C or C++ background. Rust
will output a binary executable. You can see it with `ls`:
This is similar to `gcc` or `clang`, if you come from a C or C++ background.
Rust will output a binary executable. We can see it with `ls`:
```bash
$ ls
@ -150,20 +151,20 @@ $ ./main # or main.exe on Windows
This prints out our `Hello, world!` text to our terminal.
If you come from a dynamic language like Ruby, Python, or JavaScript,
you may not be used to these two steps being separate. Rust is an
ahead-of-time compiled language, which means that you can compile a program,
give it to someone else, and they don't need to have Rust installed. If you
give someone a `.rb` or `.py` or `.js` file, they need to have a
Ruby/Python/JavaScript implementation installed, but you just need one command
to both compile and run your program. Everything is a tradeoff in language
design, and Rust has made its choice.
If you come from a dynamic language like Ruby, Python, or JavaScript, you may
not be used to these two steps being separate. Rust is an ahead-of-time
compiled language, which means that we can compile a program, give it to
someone else, and they don't need to have Rust installed. If we give someone a
`.rb` or `.py` or `.js` file, they need to have a Ruby/Python/JavaScript
implementation installed, but we just need one command to both compile and run
our program. Everything is a tradeoff in language design, and Rust has made its
choice.
Congratulations! You have officially written a Rust program. That makes you a
Rust programmer! Welcome. 🎊🎉👍
Next, I'd like to introduce you to another tool, Cargo, which is used to write
real-world Rust programs. Just using `rustc` is nice for simple things, but as
your project grows, you'll want something to help you manage all of the options
that it has, and to make it easy to share your code with other people and
our project grows, we'll want something to help us manage all of the options
that it has, and to make it easy to share our code with other people and
projects.

View File

@ -1,21 +1,21 @@
% Installing Rust
The first step to using Rust is to install it! There are a number of ways to
install Rust, but the easiest is to use the `rustup` script. If you're on Linux
or a Mac, all you need to do is this:
install Rust, but the easiest is to use the `rustup` script. If we're on Linux
or a Mac, all we need to do is this:
> Note: you don't need to type in the `$`s, they just indicate the start of
> each command. Youll see many tutorials and examples around the web that
> follow this convention: `$` for commands run as your regular user, and
> `#` for commands you should be running as an administrator.
> Note: we don't need to type in the `$`s, they just indicate the start of
> each command. Well see many tutorials and examples around the web that
> follow this convention: `$` for commands run as our regular user, and `#` for
> commands we should be running as an administrator.
```bash
$ curl -sf -L https://static.rust-lang.org/rustup.sh | sh
```
If you're concerned about the [potential insecurity][insecurity] of using `curl
| sh`, please keep reading and see our disclaimer below. And feel free to
use a two-step version of the installation and examine our installation script:
If we're concerned about the [potential insecurity][insecurity] of using `curl |
sh`, please keep reading and see our disclaimer below. And feel free to use a
two-step version of the installation and examine our installation script:
```bash
$ curl -f -L https://static.rust-lang.org/rustup.sh -O
@ -25,11 +25,12 @@ $ sh rustup.sh
[insecurity]: http://curlpipesh.tumblr.com
If you're on Windows, please download the appropriate [installer][install-page].
**NOTE:** By default, the Windows installer will not add Rust to the %PATH%
system variable. If this is the only version of Rust you are installing and you
want to be able to run it from the command line, click on "Advanced" on the
install dialog and on the "Product Features" page ensure "Add to PATH" is
installed on the local hard drive.
> Note: By default, the Windows installer won't add Rust to the %PATH% system
> variable. If this is the only version of Rust we are installing and we want to
> be able to run it from the command line, click on "Advanced" on the install
> dialog and on the "Product Features" page ensure "Add to PATH" is installed on
> the local hard drive.
[install-page]: https://www.rust-lang.org/install.html
@ -37,21 +38,21 @@ installed on the local hard drive.
## Uninstalling
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 run the uninstall
script:
Not every programming language is great for everyone. We'll just run the
uninstall script:
```bash
$ sudo /usr/local/lib/rustlib/uninstall.sh
```
If you used the Windows installer, just re-run the `.msi` and it will give you
an uninstall option.
If we used the Windows installer, we'll just re-run the `.msi` and it will give
us an uninstall option.
## That disclaimer we promised
Some people, and somewhat rightfully so, get very upset when we tell you to
`curl | sh`. Basically, when you do this, you are trusting that the good
people who maintain Rust aren't going to hack your computer and do bad things.
Some people, and somewhat rightfully so, get very upset when we tell them to
`curl | sh`. Basically, when they do this, they are trusting that the good
people who maintain Rust aren't going to hack their computer and do bad things.
That's a good instinct! If you're one of those people, please check out the
documentation on [building Rust from Source][from-source], or [the official
binary downloads][install-page].
@ -62,7 +63,7 @@ binary downloads][install-page].
Oh, we should also mention the officially supported platforms:
* Windows (7, 8, Server 2008 R2)
* Windows (7 or later, Server 2008 R2)
* Linux (2.6.18 or later, various distributions), x86 and x86-64
* OSX 10.7 (Lion) or later, x86 and x86-64
@ -73,12 +74,12 @@ testing.
Finally, a comment about Windows. Rust considers Windows to be a first-class
platform upon release, but if we're honest, the Windows experience isn't as
integrated as the Linux/OS X experience is. We're working on it! If anything
does not work, it is a bug. Please let us know if that happens. Each and every
doesn't work, it is a bug. Please let us know if that happens. Each and every
commit is tested against Windows just like any other platform.
## After installation
If you've got Rust installed, you can open up a shell, and type this:
If we've got Rust installed, we can open up a shell, and type this:
```bash
$ rustc --version
@ -93,17 +94,16 @@ variable. If it isn't, run the installer again, select "Change" on the "Change,
repair, or remove installation" page and ensure "Add to PATH" is installed on
the local hard drive.
This installer also installs a copy of the documentation locally, so you can
read it offline. On UNIX systems, `/usr/local/share/doc/rust` is the location.
On Windows, it's in a `share/doc` directory, inside wherever you installed Rust
to.
This installer also installs a copy of the documentation locally, so we can read
it offline. On UNIX systems, `/usr/local/share/doc/rust` is the location. On
Windows, it's in a `share/doc` directory, inside the directory to which Rust was
installed.
If not, there are a number of places where you can get help. The easiest is
[the #rust IRC channel on irc.mozilla.org][irc], which you can access through
[Mibbit][mibbit]. Click that link, and you'll be chatting with other Rustaceans
(a silly nickname we call ourselves), and we can help you out. Other great
resources include [the users forum][users], and
[Stack Overflow][stackoverflow].
If not, there are a number of places where we can get help. The easiest is
[the #rust IRC channel on irc.mozilla.org][irc], which we can access through
[Mibbit][mibbit]. Click that link, and we'll be chatting with other Rustaceans
(a silly nickname we call ourselves) who can help us out. Other great resources
include [the users forum][users], and [Stack Overflow][stackoverflow].
[irc]: irc://irc.mozilla.org/#rust
[mibbit]: http://chat.mibbit.com/?server=irc.mozilla.org&channel=%23rust