@@ -7,7 +7,7 @@ we’ll talk about Cargo, Rust’s build system and package manager.
7
7
# Installing Rust
8
8
9
9
The first step to using Rust is to install it. Generally speaking, you’ll need
10
- an internet connection to run the commands in this chapter, as we’ll be
10
+ an Internet connection to run the commands in this chapter, as we’ll be
11
11
downloading Rust from the internet.
12
12
13
13
We’ll be showing off a number of commands using a terminal, and those lines all
@@ -108,7 +108,7 @@ tier 3 platforms that will ever be!
108
108
If we're on Linux or a Mac, all we need to do is open a terminal and type this:
109
109
110
110
``` bash
111
- $ curl -sf -L https://static.rust-lang.org/rustup.sh | sh
111
+ $ curl -sSf https://static.rust-lang.org/rustup.sh | sh
112
112
```
113
113
114
114
This will download a script, and stat the installation. If it all goes well,
@@ -140,8 +140,7 @@ If you're on Windows, please download the appropriate [installer][install-page].
140
140
141
141
## Uninstalling
142
142
143
- If you ever need to uninstall Rust for any reason, refer to this section to
144
- find out how. It's actually as easy as installing. On Linux or Mac, just run
143
+ Uninstalling Rust is as easy as installing it. On Linux or Mac, just run
145
144
the uninstall script:
146
145
147
146
``` bash
@@ -163,11 +162,6 @@ You should see the version number, commit hash, and commit date.
163
162
164
163
If you do, Rust has been installed successfully! Congrats!
165
164
166
- This installer also installs a copy of the documentation locally, so we can
167
- read it offline. On UNIX systems, ` /usr/local/share/doc/rust ` is the location.
168
- On Windows, it's in a ` share/doc ` directory, inside the directory to which Rust
169
- was installed.
170
-
171
165
If you don't and you're on Windows, check that Rust is in your %PATH% system
172
166
variable. If it isn't, run the installer again, select "Change" on the "Change,
173
167
repair, or remove installation" page and ensure "Add to PATH" is installed on
@@ -184,6 +178,11 @@ include [the user’s forum][users], and [Stack Overflow][stackoverflow].
184
178
[ users ] : https://users.rust-lang.org/
185
179
[ stackoverflow ] : http://stackoverflow.com/questions/tagged/rust
186
180
181
+ This installer also installs a copy of the documentation locally, so we can
182
+ read it offline. On UNIX systems, ` /usr/local/share/doc/rust ` is the location.
183
+ On Windows, it's in a ` share/doc ` directory, inside the directory to which Rust
184
+ was installed.
185
+
187
186
# Hello, world!
188
187
189
188
Now that you have Rust installed, we'll help you write your first Rust program.
@@ -227,7 +226,7 @@ $ cd hello_world
227
226
228
227
## Writing and Running a Rust Program
229
228
230
- Next, make a new source file next and call it * main.rs* . Rust files always end
229
+ Next, make a new source file and call it * main.rs* . Rust files always end
231
230
in a * .rs* extension. If you’re using more than one word in your filename, use
232
231
an underscore to separate them; for example, you'd use * hello_world.rs* rather
233
232
than * helloworld.rs* .
@@ -266,28 +265,26 @@ fn main() {
266
265
```
267
266
268
267
These lines define a * function* in Rust. The ` main ` function is special: it's
269
- the beginning of every Rust program. The first line says, " I’m declaring a
270
- function named ` main ` that currently takes no arguments and returns nothing."
271
- If there were arguments, they would go inside the parentheses (` ( ` and ` ) ` ),
272
- and because we aren’t returning anything from this function, we can omit the
273
- return type entirely.
268
+ the beginning of every Rust program. The first line says, “ I’m declaring a
269
+ function named ` main ` that takes no arguments and returns nothing.” If there
270
+ were arguments, they would go inside the parentheses (` ( ` and ` ) ` ), and because
271
+ we aren’t returning anything from this function, we can omit the return type
272
+ entirely.
274
273
275
274
Also note that the function body is wrapped in curly braces (` { ` and ` } ` ). Rust
276
275
requires these around all function bodies. It's considered good style to put
277
276
the opening curly brace on the same line as the function declaration, with one
278
277
space in between.
279
278
280
- Inside the ` main() ` function, is this line :
279
+ Inside the ` main() ` function:
281
280
282
281
``` rust
283
282
println! (" Hello, world!" );
284
283
```
285
284
286
285
This line does all of the work in this little program: it prints text to the
287
286
screen. There are a number of details that are important here. The first is
288
- that it’s indented with four spaces, not tabs. If you configure your editor of
289
- choice to insert four spaces with the tab key, it will make your coding much
290
- more efficient.
287
+ that it’s indented with four spaces, not tabs.
291
288
292
289
The second important part is the ` println!() ` line. This is calling a Rust
293
290
* [ macro] * , which is how metaprogramming is done in Rust. If it were calling a
@@ -304,7 +301,7 @@ complicated topic in a systems programming language, and this is a *[statically
304
301
allocated] * string. We pass this string as an argument to ` println! ` , which
305
302
prints the string to the screen. Easy enough!
306
303
307
- [ allocation ] : the-stack-and-the-heap.html
304
+ [ statically allocated ] : the-stack-and-the-heap.html
308
305
309
306
The line ends with a semicolon (` ; ` ). Rust is an * [ expression oriented] *
310
307
language, which means that most things are expressions, rather than statements.
@@ -343,7 +340,7 @@ $ dir
343
340
main.exe main.rs
344
341
```
345
342
346
- This would create two files: the source code, with a ` .rs ` extension, and the
343
+ This shows we have two files: the source code, with an ` .rs ` extension, and the
347
344
executable (` main.exe ` on Windows, ` main ` everywhere else). All that's left to
348
345
do from here is run the ` main ` or ` main.exe ` file, like this:
349
346
@@ -361,7 +358,7 @@ give it to someone else, and they can run it even without Rust installed. If
361
358
you give someone a ` .rb ` or ` .py ` or ` .js ` file, on the other hand, they need
362
359
to have a Ruby, Python, or JavaScript implementation installed (respectively),
363
360
but you only need one command to both compile and run your program. Everything
364
- is a tradeoff in language design, and Rust has made its choice .
361
+ is a tradeoff in language design.
365
362
366
363
Just compiling with ` rustc ` is fine for simple programs, but as your project
367
364
grows, you'll want to be able to manage all of the options your project has,
@@ -374,7 +371,7 @@ programs.
374
371
Cargo is Rust’s build system and package manager, and Rustaceans use Cargo to
375
372
manage their Rust projects. Cargo manages three things: building your code,
376
373
downloading the libraries your code depends on, and building those libraries.
377
- We call libraries your code needs ‘dependencies’, since your code depends on
374
+ We call libraries your code needs ‘dependencies’ since your code depends on
378
375
them.
379
376
380
377
The simplest Rust programs don’t have any dependencies, so right now, you'd
@@ -442,9 +439,8 @@ Make sure to capitalize the `C` in `Cargo.toml`, or Cargo won't know what to do
442
439
with the configuration file.
443
440
444
441
This file is in the * [ TOML] * (Tom's Obvious, Minimal Language) format. TOML is
445
- similar to INI, but has some extra goodies. According to the TOML docs, TOML
446
- “aims to be a minimal configuration file format that's easy to read”, and so we
447
- chose it as the format Cargo uses.
442
+ similar to INI, but has some extra goodies, and is used as Cargo’s
443
+ configuration format.
448
444
449
445
[ TOML ] : https://github.com/toml-lang/toml
450
446
@@ -494,9 +490,9 @@ Hello, world!
494
490
```
495
491
496
492
Notice that this example didn’t re-build the project. Cargo figured out that
497
- the hasn’t changed, and so it just ran the binary. If you'd modified your
498
- program , Cargo would have built the file before running it, and you would have
499
- seen something like this:
493
+ the file hasn’t changed, and so it just ran the binary. If you'd modified your
494
+ source code , Cargo would have rebuilt the project before running it, and you
495
+ would have seen something like this:
500
496
501
497
``` bash
502
498
$ cargo run
@@ -509,9 +505,9 @@ Cargo checks to see if any of your project’s files have been modified, and onl
509
505
rebuilds your project if they’ve changed since the last time you built it.
510
506
511
507
With simple projects, Cargo doesn't bring a whole lot over just using ` rustc ` ,
512
- but it will become useful in future. When your projects get more complex,
513
- you'll need to do more things to get all of the parts to properly compile. With
514
- Cargo, you can just run ` cargo build ` , and it should work the right way.
508
+ but it will become useful in future. With complex projects composed of multiple
509
+ crates, it’s much easier to let Cargo coordinate the build. With Cargo, you can
510
+ just run ` cargo build ` , and it should work the right way.
515
511
516
512
## Building for Release
517
513
0 commit comments