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 your code, downloading the dependencies
11 your code needs, and building those dependencies. At first, your program doesn’t
12 have any dependencies, so we’ll only be using the first part of its
13 functionality. Eventually, we’ll add more. Since we started off by using Cargo,
14 it'll be easy to add later.
16 If we installed Rust via the official installers we will also have Cargo. If we
17 installed Rust some other way, we may want to [check the Cargo
18 README][cargoreadme] for specific instructions about installing it.
20 [cargoreadme]: https://github.com/rust-lang/cargo#installing-cargo-from-nightlies
22 ## Converting to Cargo
24 Let’s convert Hello World to Cargo.
26 To Cargo-ify our project, we need to do three things: Make a `Cargo.toml`
27 configuration file, put our source file in the right place, and get rid of the
28 old executable (`main.exe` on Windows, `main` everywhere else). Let's do that part first:
32 $ mv main.rs src/main.rs
33 $ rm main # or main.exe on Windows
36 Note that since we're creating an executable, we retain `main.rs` as the source
37 filename. If we want to make a library instead, we should use `lib.rs`. This
38 convention is used by Cargo to successfully compile our projects, but it can be
39 overridden if we wish. Custom file locations for the entry point can be
40 specified with a [`[lib]` or `[[bin]]`][crates-custom] key in the TOML file.
42 [crates-custom]: http://doc.crates.io/manifest.html#configuring-a-target
44 Cargo expects your source files to live inside a `src` directory. That leaves
45 the top level for other things, like READMEs, license information, and anything
46 not related to your code. Cargo helps us keep our projects nice and tidy. A
47 place for everything, and everything in its place.
49 Next, our configuration file:
55 Make sure to get this name right: you need the capital `C`!
64 authors = [ "Your name <you@example.com>" ]
67 This file is in the [TOML][toml] format. TOML is similar to INI, but has some
68 extra goodies. According to the TOML docs,
70 > TOML aims to be a minimal configuration file format that's easy to read due
71 > to obvious semantics. TOML is designed to map unambiguously to a hash table.
72 > TOML should be easy to parse into data structures in a wide variety of
75 [toml]: https://github.com/toml-lang/toml
77 Once we have this file in place in our project's root directory, we should be
78 ready to build! To do so, run:
82 Compiling hello_world v0.0.1 (file:///home/yourname/projects/hello_world)
83 $ ./target/debug/hello_world
87 Bam! We built our project with `cargo build`, and ran it with
88 `./target/debug/hello_world`. We can do both in one step with `cargo run`:
92 Running `target/debug/hello_world`
96 Notice that we didn’t re-build the project this time. Cargo figured out that
97 we hadn’t changed the source file, and so it just ran the binary. If we had
98 made a modification, we would have seen it do both:
102 Compiling hello_world v0.0.1 (file:///home/yourname/projects/hello_world)
103 Running `target/debug/hello_world`
107 This hasn’t bought us a whole lot over our simple use of `rustc`, but think
108 about the future: when our project gets more complex, we need to do more
109 things to get all of the parts to properly compile. With Cargo, as our project
110 grows, we can just run `cargo build`, and it’ll work the right way.
112 When your project is finally ready for release, you can use
113 `cargo build --release` to compile your project with optimizations.
115 You'll also notice that Cargo has created a new file: `Cargo.lock`.
123 The `Cargo.lock` file is used by Cargo to keep track of dependencies in your application.
124 Right now, we don’t have any, so it’s a bit sparse. You won't ever need
125 to touch this file yourself, just let Cargo handle it.
127 That’s it! We’ve successfully built `hello_world` with Cargo. Even though our
128 program is simple, it’s using much of the real tooling that you’ll use for the
129 rest of your Rust career. You can expect to do this to get started with
130 virtually all Rust projects:
133 $ git clone someurl.com/foo
140 You don’t have to go through this whole process every time you want to start a
141 new project! Cargo has the ability to make a bare-bones project directory in
142 which you can start developing right away.
144 To start a new project with Cargo, use `cargo new`:
147 $ cargo new hello_world --bin
150 We’re 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 you’re on a Unix system)
152 Let's check out what Cargo has generated for us:
165 If you don't have the `tree` command, you can probably get it from your
166 distribution’s package manager. It’s not necessary, but it’s certainly useful.
168 This is all we need to get started. First, let’s check out `Cargo.toml`:
175 authors = ["Your Name <you@example.com>"]
178 Cargo has populated this file with reasonable defaults based off the arguments
179 you gave it and your `git` global configuration. You may notice that Cargo has
180 also initialized the `hello_world` directory as a `git` repository.
182 Here’s what’s in `src/main.rs`:
186 println!("Hello, world!");
190 Cargo has generated a "Hello World!" for us, and you’re ready to start coding! Cargo
191 has its own [guide][guide] which covers Cargo’s features in much more depth.
193 [guide]: http://doc.crates.io/guide.html
195 Now that you’ve got the tools down, let’s actually learn more about the Rust
196 language itself. These are the basics that will serve you well through the rest
197 of your time with Rust.
199 You have two options: Dive into a project with ‘[Learn Rust][learnrust]’, or
200 start from the bottom and work your way up with ‘[Syntax and
201 Semantics][syntax]’. More experienced systems programmers will probably prefer
202 ‘Learn Rust’, while those from dynamic backgrounds may enjoy either. Different
203 people learn differently! Choose whatever’s right for you.
205 [learnrust]: learn-rust.html
206 [syntax]: syntax-and-semantics.html