@@ -27,7 +27,8 @@ Sound good? Let's go!
2727
2828The first step to using Rust is to install it! There are a number of ways to
2929install 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):
3132
3233``` {ignore}
3334$ curl -s http://www.rust-lang.org/rustup.sh | sudo sh
@@ -96,13 +97,14 @@ host: x86_64-unknown-linux-gnu
9697If you did, Rust has been installed successfully! Congrats!
9798
9899If 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
101103that 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 ) .
106108
107109## Hello, world!
108110
@@ -123,8 +125,7 @@ require that you know a whole ton about the command line, but until the
123125language is in a more finished state, IDE support is spotty. Rust makes no
124126specific demands on your editing tooling, or where your code lives.
125127
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.
128129
129130``` {bash}
130131$ mkdir ~/projects
@@ -159,7 +160,7 @@ Save the file, and then type this into your terminal window:
159160
160161``` {bash}
161162$ rustc hello_world.rs
162- $ ./hello_world # on Windows, this is ./hello_world.exe
163+ $ ./hello_world # just 'hello_world' on Windows
163164Hello, world
164165```
165166
@@ -180,8 +181,8 @@ entirely. We'll get to it later.
180181
181182You'll also note that the function is wrapped in curly braces (` { ` and ` } ` ).
182183Rust 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.
185186
186187Next up is this line:
187188
@@ -199,13 +200,16 @@ The second point is the `println!()` part. This is calling a Rust **macro**,
199200which is how metaprogramming is done in Rust. If it were a function instead, it
200201would look like this: ` println() ` . For our purposes, we don't need to worry
201202about 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.
203207
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!
209213
210214Finally, the line ends with a semicolon (` ; ` ). Rust is an ** expression
211215oriented** language, which means that most things are expressions. The ` ; ` is
@@ -235,8 +239,8 @@ $ dir
235239hello_world.exe hello_world.rs
236240```
237241
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)
240244
241245``` {bash}
242246$ ./hello_world # or ./hello_world.exe on Windows
@@ -264,26 +268,146 @@ projects.
264268
265269## Hello, Cargo!
266270
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.
267276
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?
268281
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.
269287
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.
270290
291+ First, let's go back to our projects directory. We don't want Cargo to
292+ live in our project!
271293
294+ ``` {bash}
295+ $ cd ..
296+ ```
272297
298+ Next, we need these commands:
273299
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+ ```
274306
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 ` .
275313
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.
276316
317+ Let's see if that worked. Try this:
277318
319+ ``` {bash}
320+ $ cargo
321+ Commands:
322+ build # compile the current project
278323
324+ Options (for all commands):
279325
326+ -v, [--verbose]
327+ -h, [--help]
328+ ```
280329
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.
281333
334+ Let's move back into our ` hello_world ` directory now:
282335
336+ ``` {bash}
337+ $ cd .. # move back up into projects
338+ $ cd hello_world # move into hello_world
339+ ```
283340
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:
284344
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+ ```
285360
361+ Make sure to get this name right: you need the capital ` C ` !
286362
363+ Put this inside:
364+
365+ ``` {ignore}
366+ [package]
367+
368+ name = "hello_world"
369+ version = "0.1.0"
370+ authors = [ "someone@example.com" ]
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+ ```
287400
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.
288407
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.
289411
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