3 [Cargo][cratesio] is a tool that Rustaceans use to help manage their Rust
4 projects. Cargo is currently in a pre-1.0 state, and so it is still a work in
5 progress. However, it is already good enough to use for many Rust projects, and
6 so it is assumed that Rust projects will use Cargo from the beginning.
8 [cratesio]: http://doc.crates.io
10 Cargo manages three things: building our code, downloading the dependencies our
11 code needs, and building those dependencies. At first, our program doesn’t have
12 any dependencies, so we’ll only be using the first part of its functionality.
13 Eventually, we’ll add more. Since we started off by using Cargo, it'll be easy
16 If you installed Rust via the official installers you will also have Cargo. If
17 you installed Rust some other way, you may want to
18 [check the Cargo README][cargoreadme] for specific instructions about installing
21 [cargoreadme]: https://github.com/rust-lang/cargo#installing-cargo-from-nightlies
23 ## Converting to Cargo
25 Let’s convert Hello World to Cargo.
27 To Cargo-ify our project, we need to do three things: Make a `Cargo.toml`
28 configuration file, put our source file in the right place, and get rid of the
29 old executable (`main.exe` on Windows, `main` everywhere else). Let's do that part first:
33 $ mv main.rs src/main.rs
34 $ rm main # or 'rm main.exe' on Windows
37 > Note: since we're creating an executable, we retain `main.rs` as the source
38 > filename. If we want to make a library instead, we should use `lib.rs`. This
39 > convention is used by Cargo to successfully compile our projects, but it can
40 > be overridden if we wish. Custom file locations for the entry point can be
41 > specified with a [`[lib]` or `[[bin]]`][crates-custom] key in the TOML file.
43 [crates-custom]: http://doc.crates.io/manifest.html#configuring-a-target
45 Cargo expects our source files to live inside a `src` directory. That leaves the
46 top level for other things, like READMEs, license information, and anything not
47 related to our code. Cargo helps us keep our projects nice and tidy. A place for
48 everything, and everything in its place.
50 Next, our configuration file:
53 $ editor Cargo.toml # or 'notepad Cargo.toml' on Windows
56 Make sure to get this name right: we need the capital `C`!
65 authors = [ "Your name <you@example.com>" ]
68 This file is in the [TOML][toml] format. TOML is similar to INI, but has some
69 extra goodies. According to the TOML docs,
71 > TOML aims to be a minimal configuration file format that's easy to read due
72 > to obvious semantics. TOML is designed to map unambiguously to a hash table.
73 > TOML should be easy to parse into data structures in a wide variety of
76 [toml]: https://github.com/toml-lang/toml
78 Once we have this file in place in our project's root directory, we should be
79 ready to build! To do so, run:
83 Compiling hello_world v0.0.1 (file:///home/yourname/projects/hello_world)
84 $ ./target/debug/hello_world
88 Bam! We built our project with `cargo build`, and ran it with
89 `./target/debug/hello_world`. We can do both in one step with `cargo run`:
93 Running `target/debug/hello_world`
97 Notice that we didn’t re-build the project this time. Cargo figured out that
98 we hadn’t changed the source file, and so it just ran the binary. If we had
99 made a modification, we would have seen it do both:
103 Compiling hello_world v0.0.1 (file:///home/yourname/projects/hello_world)
104 Running `target/debug/hello_world`
108 This hasn’t bought us a whole lot over our simple use of `rustc`, but think
109 about the future: when our project gets more complex, we need to do more
110 things to get all of the parts to properly compile. With Cargo, as our project
111 grows, we can just run `cargo build`, and it’ll work the right way.
113 When our project is finally ready for release, we can use `cargo build
114 --release` to compile our project with optimizations.
116 You'll also notice that Cargo has created a new file: `Cargo.lock`.
124 The `Cargo.lock` file is used by Cargo to keep track of dependencies in our
125 application. Right now, we don’t have any, so it’s a bit sparse. We won't ever
126 need to touch this file ourselves, just let Cargo handle it.
128 That’s it! We’ve successfully built `hello_world` with Cargo. Even though our
129 program is simple, it’s using much of the real tooling that we’ll use for the
130 rest of our Rust career. We can expect to do this to get started with virtually
134 $ git clone someurl.com/foo
141 We don’t have to go through this whole process every time we want to start a new
142 project! Cargo has the ability to make a bare-bones project directory in which
143 we can start developing right away.
145 To start a new project with Cargo, we use `cargo new`:
148 $ cargo new hello_world --bin
151 We’re passing `--bin` because our goal is to get straight to making an
152 executable application, as opposed to a library. Executables are often called
153 ‘binaries.’ (as in `/usr/bin`, if we’re on a Unix system)
155 Let's check out what Cargo has generated for us:
168 If we don't have the `tree` command, we can probably get it from our
169 distribution’s package manager. It’s not necessary, but it’s certainly useful.
171 This is all we need to get started. First, let’s check out `Cargo.toml`:
178 authors = ["Your Name <you@example.com>"]
181 Cargo has populated this file with reasonable defaults based off the arguments
182 we gave it and our `git` global configuration. You may notice that Cargo has
183 also initialized the `hello_world` directory as a `git` repository.
185 Here’s what’s in `src/main.rs`:
189 println!("Hello, world!");
193 Cargo has generated a "Hello World!" for us, and we’re ready to start coding!
194 Cargo has its own [guide][guide] which covers Cargo’s features in much more
197 [guide]: http://doc.crates.io/guide.html
199 Now that we’ve got the tools down, let’s actually learn more about the Rust
200 language itself. These are the basics that will serve us well through the rest
201 of our time with Rust.
203 You have two options: Dive into a project with ‘[Learn Rust][learnrust]’, or
204 start from the bottom and work your way up with
205 ‘[Syntax and Semantics][syntax]’. More experienced systems programmers will
206 probably prefer ‘Learn Rust’, while those from dynamic backgrounds may enjoy
207 either. Different people learn differently! Choose whatever’s right for you.
209 [learnrust]: learn-rust.html
210 [syntax]: syntax-and-semantics.html