@@ -27,7 +27,8 @@ Sound good? Let's go!
27
27
28
28
The first step to using Rust is to install it! There are a number of ways to
29
29
install Rust, but the easiest is to use the the ` rustup ` script. If you're on
30
- Linux or a Mac, All you need to do is this:
30
+ Linux or a Mac, all you need to do is this (note that you don't need to type
31
+ in the ` $ ` s, they just indicate the start of each command):
31
32
32
33
``` {ignore}
33
34
$ curl -s http://www.rust-lang.org/rustup.sh | sudo sh
@@ -96,13 +97,14 @@ host: x86_64-unknown-linux-gnu
96
97
If you did, Rust has been installed successfully! Congrats!
97
98
98
99
If not, there are a number of places where you can get help. The easiest is
99
- IRC, which you can access
100
- [ here] ( http://chat.mibbit.com/?server=irc.mozilla.org&channel=%23rust ) . Click
100
+ [ the #rust IRC channel on irc.mozilla.org] ( irc://irc.mozilla.org/#rust ) , which
101
+ you can access through
102
+ [ Mibbit] ( http://chat.mibbit.com/?server=irc.mozilla.org&channel=%23rust ) . Click
101
103
that link, and you'll be chatting with other Rustaceans (a silly nickname we
102
- call ourselves), and we can help you out. Other great resources include our
103
- [ mailing list] ( https://mail.mozilla.org/listinfo/rust-dev ) ,
104
- [ subreddit] ( http://www.reddit.com/r/rust ) , and
105
- [ StackOverflow ] ( http://stackoverflow.com/questions/tagged/rust ) .
104
+ call ourselves), and we can help you out. Other great resources include [ our
105
+ mailing list] ( https://mail.mozilla.org/listinfo/rust-dev ) , [ the /r/rust
106
+ subreddit] ( http://www.reddit.com/r/rust ) , and [ Stack
107
+ Overflow ] ( http://stackoverflow.com/questions/tagged/rust ) .
106
108
107
109
## Hello, world!
108
110
@@ -123,8 +125,7 @@ require that you know a whole ton about the command line, but until the
123
125
language is in a more finished state, IDE support is spotty. Rust makes no
124
126
specific demands on your editing tooling, or where your code lives.
125
127
126
- With that said, let's make a directory in our projects directory. Note that you
127
- don't need to type in the ` $ ` s, they just indicate the start of each command:
128
+ With that said, let's make a directory in our projects directory.
128
129
129
130
``` {bash}
130
131
$ mkdir ~/projects
@@ -159,7 +160,7 @@ Save the file, and then type this into your terminal window:
159
160
160
161
``` {bash}
161
162
$ rustc hello_world.rs
162
- $ ./hello_world # on Windows, this is ./hello_world.exe
163
+ $ ./hello_world # just 'hello_world' on Windows
163
164
Hello, world
164
165
```
165
166
@@ -180,8 +181,8 @@ entirely. We'll get to it later.
180
181
181
182
You'll also note that the function is wrapped in curly braces (` { ` and ` } ` ).
182
183
Rust requires these around all function bodies. It is also considered good
183
- style to put the curly brace on the same line as the function declaration, with
184
- one space in between.
184
+ style to put the opening curly brace on the same line as the function
185
+ declaration, with one space in between.
185
186
186
187
Next up is this line:
187
188
@@ -199,13 +200,16 @@ The second point is the `println!()` part. This is calling a Rust **macro**,
199
200
which is how metaprogramming is done in Rust. If it were a function instead, it
200
201
would look like this: ` println() ` . For our purposes, we don't need to worry
201
202
about this difference. Just know that sometimes, you'll see a ` ! ` , and that
202
- means that you're calling a macro instead of a normal function.
203
+ means that you're calling a macro instead of a normal function. One last thing
204
+ to mention: Rust's macros are significantly different than C macros, if you've
205
+ used those. Don't be scared of using macros. We'll get to the details
206
+ eventually, you'll just have to trust us for now.
203
207
204
- Next, ` "Hello, world" ` is a ** string** . Strings are a surprisingly
205
- complicated topic in a systems programming language, and this is a ** staticly
206
- allocated ** string. We will talk more about different kinds of allocation
207
- later. We pass this string as an argument to ` println! ` , which prints the
208
- string to the screen. Easy enough!
208
+ Next, ` "Hello, world" ` is a ** string** . Strings are a surprisingly complicated
209
+ topic in a systems programming language, and this is a ** statically allocated **
210
+ string. We will talk more about different kinds of allocation later. We pass
211
+ this string as an argument to ` println! ` , which prints the string to the
212
+ screen. Easy enough!
209
213
210
214
Finally, the line ends with a semicolon (` ; ` ). Rust is an ** expression
211
215
oriented** language, which means that most things are expressions. The ` ; ` is
@@ -235,8 +239,8 @@ $ dir
235
239
hello_world.exe hello_world.rs
236
240
```
237
241
238
- There are now two files: our source code, with the ` .rs ` , and the executable.
239
- We ran the executable like this:
242
+ There are now two files: our source code, with the ` .rs ` extension , and the
243
+ executable ( ` hello_world.exe ` on Windows, ` hello_world ` everywhere else)
240
244
241
245
``` {bash}
242
246
$ ./hello_world # or ./hello_world.exe on Windows
@@ -264,26 +268,146 @@ projects.
264
268
265
269
## Hello, Cargo!
266
270
271
+ [ Cargo] ( http://crates.io ) is a tool that Rustaceans use to help manage their
272
+ Rust projects. Cargo is currently in an alpha state, just like Rust, and so it
273
+ is still a work in progress. However, it is already good enough to use for many
274
+ Rust projects, and so it is assumed that Rust projects will use Cargo from the
275
+ beginning.
267
276
277
+ Programmers love car analogies, so I've got a good one for you to think about
278
+ the relationship between ` cargo ` and ` rustc ` : ` rustc ` is like a car, and
279
+ ` cargo ` is like a robotic driver. You can drive your car yourself, of course,
280
+ but isn't it just easier to let a computer drive it for you?
268
281
282
+ Anyway, Cargo manages three things: building your code, downloading the
283
+ dependencies your code needs, and building the dependencies your code needs.
284
+ At first, your program doesn't have any dependencies, so we'll only be using
285
+ the first part of its functionality. Eventually, we'll add more. Since we
286
+ started off by using Cargo, it'll be easy to add later.
269
287
288
+ Let's convert Hello World to Cargo. The first thing we need to do is install
289
+ it. To do this, we need to build it from source. There are no binaries yet.
270
290
291
+ First, let's go back to our projects directory. We don't want Cargo to
292
+ live in our project!
271
293
294
+ ``` {bash}
295
+ $ cd ..
296
+ ```
272
297
298
+ Next, we need these commands:
273
299
300
+ ``` {bash}
301
+ $ git clone --recursive https://github.com/rust-lang/cargo
302
+ $ cd cargo
303
+ $ make
304
+ $ make install # may need sudo or admin permissions
305
+ ```
274
306
307
+ The ` --recursive ` downloads Cargo's own dependencies. You can't use Cargo to
308
+ fetch dependencies until you have Cargo installed! Also, you will need to have
309
+ ` git ` installed. Much of the Rust world assumes ` git ` usage, so it's a good
310
+ thing to have around. Please check out [ the git
311
+ documentation] ( http://git-scm.com/book/en/Getting-Started-Installing-Git ) for
312
+ more on installing ` git ` .
275
313
314
+ We hope to give Cargo a binary installer, similar to Rust's own, so that
315
+ this will not be necessary in the future.
276
316
317
+ Let's see if that worked. Try this:
277
318
319
+ ``` {bash}
320
+ $ cargo
321
+ Commands:
322
+ build # compile the current project
278
323
324
+ Options (for all commands):
279
325
326
+ -v, [--verbose]
327
+ -h, [--help]
328
+ ```
280
329
330
+ If you see this output when you run ` cargo ` , congrats! Cargo is working. If
331
+ not, please [ open an issue] ( https://github.com/rust-lang/cargo/issues/new ) or
332
+ drop by the Rust IRC, and we can help you out.
281
333
334
+ Let's move back into our ` hello_world ` directory now:
282
335
336
+ ``` {bash}
337
+ $ cd .. # move back up into projects
338
+ $ cd hello_world # move into hello_world
339
+ ```
283
340
341
+ To Cargo-ify our project, we need to do two things: Make a ` Cargo.toml `
342
+ configuration file, and put our source file in the right place. Let's
343
+ do that part first:
284
344
345
+ ``` {bash}
346
+ $ mkdir src
347
+ $ mv hello_world.rs src/hello_world.rs
348
+ ```
349
+
350
+ Cargo expects your source files to live inside a ` src ` directory. That leaves
351
+ the top level for other things, like READMEs, licence information, and anything
352
+ not related to your code. Cargo helps us keep our projects nice and tidy. A
353
+ place for everything, and everything in its place.
354
+
355
+ Next, our configuration file:
356
+
357
+ ``` {bash}
358
+ $ editor Cargo.toml
359
+ ```
285
360
361
+ Make sure to get this name right: you need the capital ` C ` !
286
362
363
+ Put this inside:
364
+
365
+ ``` {ignore}
366
+ [package]
367
+
368
+ name = "hello_world"
369
+ version = "0.1.0"
370
+
371
+
372
+ [[bin]]
373
+
374
+ name = "hello_world"
375
+ ```
376
+
377
+ This file is in the [ TOML] ( https://github.com/toml-lang/toml ) format. Let's let
378
+ it explain itself to you:
379
+
380
+ > TOML aims to be a minimal configuration file format that's easy to read due
381
+ > to obvious semantics. TOML is designed to map unambiguously to a hash table.
382
+ > TOML should be easy to parse into data structures in a wide variety of
383
+ > languages.
384
+
385
+ TOML is very similar to INI, but with some extra goodies.
386
+
387
+ Anyway, there are two ** table** s in this file: ` package ` and ` bin ` . The first
388
+ tells Cargo metadata about your package. The second tells Cargo that we're
389
+ interested in building a binary, not a library (though we could do both!), as
390
+ well as what it is named.
391
+
392
+ Once you have this file in place, we should be ready to build! Try this:
393
+
394
+ ``` {bash}
395
+ $ cargo build
396
+ Compiling hello_world v0.1.0 (file:/home/yourname/projects/hello_world)
397
+ $ ./target/hello_world
398
+ Hello, world!
399
+ ```
287
400
401
+ Bam! We build our project with ` cargo build ` , and run it with
402
+ ` ./target/hello_world ` . This hasn't bought us a whole lot over our simple use
403
+ of ` rustc ` , but think about the future: when our project has more than one
404
+ file, we would need to call ` rustc ` twice, and pass it a bunch of options to
405
+ tell it to build everything together. With Cargo, as our project grows, we can
406
+ just ` cargo build ` and it'll work the right way.
288
407
408
+ That's it! We've successfully built ` hello_world ` with Cargo. Even though our
409
+ program is simple, it's using much of the real tooling that you'll use for the
410
+ rest of your Rust career.
289
411
412
+ Next, we'll learn more about Rust itself, by starting to write a more complicated
413
+ program. We hope you want to do more with Rust than just print "Hello, world!"
0 commit comments