]> git.proxmox.com Git - rustc.git/blame - src/doc/trpl/hello-cargo.md
Imported Upstream version 1.5.0+dfsg1
[rustc.git] / src / doc / trpl / hello-cargo.md
CommitLineData
1a4d82fc
JJ
1% Hello, Cargo!
2
9346a6ac
AL
3[Cargo][cratesio] is a tool that Rustaceans use to help manage their Rust
4projects. Cargo is currently in a pre-1.0 state, and so it is still a work in
5progress. However, it is already good enough to use for many Rust projects, and
6so it is assumed that Rust projects will use Cargo from the beginning.
7
bd371182 8[cratesio]: http://doc.crates.io
1a4d82fc 9
b039eaaf
SL
10Cargo manages three things: building our code, downloading the dependencies our
11code needs, and building those dependencies. At first, our program doesn’t have
12any dependencies, so we’ll only be using the first part of its functionality.
13Eventually, we’ll add more. Since we started off by using Cargo, it'll be easy
14to add later.
1a4d82fc 15
b039eaaf
SL
16If you installed Rust via the official installers you will also have Cargo. If
17you installed Rust some other way, you may want to
18[check the Cargo README][cargoreadme] for specific instructions about installing
19it.
9346a6ac
AL
20
21[cargoreadme]: https://github.com/rust-lang/cargo#installing-cargo-from-nightlies
1a4d82fc 22
c34b1796
AL
23## Converting to Cargo
24
9346a6ac 25Let’s convert Hello World to Cargo.
1a4d82fc 26
c1a9b12d
SL
27To Cargo-ify our project, we need to do three things: Make a `Cargo.toml`
28configuration file, put our source file in the right place, and get rid of the
29old executable (`main.exe` on Windows, `main` everywhere else). Let's do that part first:
1a4d82fc 30
c34b1796 31```bash
1a4d82fc
JJ
32$ mkdir src
33$ mv main.rs src/main.rs
b039eaaf 34$ rm main # or 'rm main.exe' on Windows
1a4d82fc
JJ
35```
36
b039eaaf
SL
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.
bd371182
AL
42
43[crates-custom]: http://doc.crates.io/manifest.html#configuring-a-target
44
b039eaaf
SL
45Cargo expects our source files to live inside a `src` directory. That leaves the
46top level for other things, like READMEs, license information, and anything not
47related to our code. Cargo helps us keep our projects nice and tidy. A place for
48everything, and everything in its place.
1a4d82fc
JJ
49
50Next, our configuration file:
51
c34b1796 52```bash
b039eaaf 53$ editor Cargo.toml # or 'notepad Cargo.toml' on Windows
1a4d82fc
JJ
54```
55
b039eaaf 56Make sure to get this name right: we need the capital `C`!
1a4d82fc
JJ
57
58Put this inside:
59
60```toml
61[package]
62
63name = "hello_world"
64version = "0.0.1"
65authors = [ "Your name <you@example.com>" ]
1a4d82fc
JJ
66```
67
c1a9b12d
SL
68This file is in the [TOML][toml] format. TOML is similar to INI, but has some
69extra goodies. According to the TOML docs,
1a4d82fc
JJ
70
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
74> languages.
75
9346a6ac 76[toml]: https://github.com/toml-lang/toml
1a4d82fc 77
c1a9b12d
SL
78Once we have this file in place in our project's root directory, we should be
79ready to build! To do so, run:
1a4d82fc 80
c34b1796 81```bash
1a4d82fc
JJ
82$ cargo build
83 Compiling hello_world v0.0.1 (file:///home/yourname/projects/hello_world)
c34b1796 84$ ./target/debug/hello_world
1a4d82fc
JJ
85Hello, world!
86```
87
62682a34 88Bam! We built our project with `cargo build`, and ran it with
9346a6ac
AL
89`./target/debug/hello_world`. We can do both in one step with `cargo run`:
90
91```bash
92$ cargo run
93 Running `target/debug/hello_world`
94Hello, world!
95```
96
97Notice that we didn’t re-build the project this time. Cargo figured out that
98we hadn’t changed the source file, and so it just ran the binary. If we had
99made a modification, we would have seen it do both:
100
101```bash
bd371182 102$ cargo run
9346a6ac
AL
103 Compiling hello_world v0.0.1 (file:///home/yourname/projects/hello_world)
104 Running `target/debug/hello_world`
105Hello, world!
106```
107
108This hasn’t bought us a whole lot over our simple use of `rustc`, but think
62682a34 109about the future: when our project gets more complex, we need to do more
9346a6ac 110things to get all of the parts to properly compile. With Cargo, as our project
62682a34 111grows, we can just run `cargo build`, and it’ll work the right way.
9346a6ac 112
b039eaaf
SL
113When our project is finally ready for release, we can use `cargo build
114--release` to compile our project with optimizations.
1a4d82fc
JJ
115
116You'll also notice that Cargo has created a new file: `Cargo.lock`.
117
118```toml
119[root]
120name = "hello_world"
121version = "0.0.1"
122```
123
b039eaaf
SL
124The `Cargo.lock` file is used by Cargo to keep track of dependencies in our
125application. Right now, we don’t have any, so it’s a bit sparse. We won't ever
126need to touch this file ourselves, just let Cargo handle it.
1a4d82fc 127
9346a6ac 128That’s it! We’ve successfully built `hello_world` with Cargo. Even though our
b039eaaf
SL
129program is simple, it’s using much of the real tooling that we’ll use for the
130rest of our Rust career. We can expect to do this to get started with virtually
131all Rust projects:
9346a6ac
AL
132
133```bash
134$ git clone someurl.com/foo
135$ cd foo
136$ cargo build
137```
1a4d82fc 138
c34b1796
AL
139## A New Project
140
b039eaaf
SL
141We don’t have to go through this whole process every time we want to start a new
142project! Cargo has the ability to make a bare-bones project directory in which
143we can start developing right away.
c34b1796 144
b039eaaf 145To start a new project with Cargo, we use `cargo new`:
c34b1796
AL
146
147```bash
148$ cargo new hello_world --bin
149```
150
b039eaaf
SL
151We’re passing `--bin` because our goal is to get straight to making an
152executable application, as opposed to a library. Executables are often called
153‘binaries.’ (as in `/usr/bin`, if we’re on a Unix system)
c34b1796
AL
154
155Let's check out what Cargo has generated for us:
156
157```bash
158$ cd hello_world
159$ tree .
160.
161├── Cargo.toml
162└── src
163 └── main.rs
164
1651 directory, 2 files
166```
167
b039eaaf 168If we don't have the `tree` command, we can probably get it from our
9346a6ac 169distribution’s package manager. It’s not necessary, but it’s certainly useful.
c34b1796 170
9346a6ac 171This is all we need to get started. First, let’s check out `Cargo.toml`:
c34b1796
AL
172
173```toml
174[package]
175
176name = "hello_world"
62682a34 177version = "0.1.0"
c34b1796
AL
178authors = ["Your Name <you@example.com>"]
179```
180
9346a6ac 181Cargo has populated this file with reasonable defaults based off the arguments
b039eaaf 182we gave it and our `git` global configuration. You may notice that Cargo has
9346a6ac 183also initialized the `hello_world` directory as a `git` repository.
c34b1796 184
9346a6ac 185Here’s what’s in `src/main.rs`:
c34b1796
AL
186
187```rust
188fn main() {
189 println!("Hello, world!");
190}
191```
192
b039eaaf
SL
193Cargo has generated a "Hello World!" for us, and we’re ready to start coding!
194Cargo has its own [guide][guide] which covers Cargo’s features in much more
195depth.
c34b1796 196
9346a6ac
AL
197[guide]: http://doc.crates.io/guide.html
198
b039eaaf
SL
199Now that we’ve got the tools down, let’s actually learn more about the Rust
200language itself. These are the basics that will serve us well through the rest
201of our time with Rust.
9346a6ac
AL
202
203You have two options: Dive into a project with ‘[Learn Rust][learnrust]’, or
b039eaaf
SL
204start from the bottom and work your way up with
205‘[Syntax and Semantics][syntax]’. More experienced systems programmers will
206probably prefer ‘Learn Rust’, while those from dynamic backgrounds may enjoy
207either. Different people learn differently! Choose whatever’s right for you.
9346a6ac
AL
208
209[learnrust]: learn-rust.html
210[syntax]: syntax-and-semantics.html