11% Hello, Cargo!
22
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.
3+ Cargo is Rust’s build system and package manager, and Rustaceans use Cargo to
4+ manage their Rust projects. Cargo manages three things: building your code,
5+ downloading the libraries your code depends on, and building those libraries.
6+ We call libraries your code needs ‘dependencies’, since your code depends on
7+ them.
8+
9+ The simplest Rust programs don’t have any dependencies, so right now, you'd
10+ only use the first part of its functionality. As you write more complex Rust
11+ programs, you’ll want to add dependencies, and if you start off using Cargo,
12+ that will be a lot easier to do.
13+
14+ As the vast, vast majority of Rust projects use Cargo, we will assume that
15+ you’re using it for the rest of the book. Cargo comes installed with Rust
16+ itself, if you used the official installers. If you installed Rust through some
17+ other means, you can check if you have Cargo installed by typing:
718
8- [ cratesio ] : http://doc.crates.io
19+ ``` bash
20+ $ cargo --version
21+ ```
922
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
14- to add later.
23+ Into a terminal. If you see a version number, great! If you see an error like
24+ ‘` command not found ` ’, then you should look at the documentation for the system
25+ in which you installed Rust, to determine if Cargo is separate.
1526
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
19- it.
27+ ## Converting to Cargo
2028
21- [ cargoreadme ] : https://github.com/rust-lang/cargo#installing-cargo-from-nightlies
29+ Let’s convert the Hello World program to Cargo. To Cargo-fy a project, you need
30+ to do three things:
2231
23- ## Converting to Cargo
32+ 1 . Put your source file in the right directory.
33+ 2 . Get rid of the old executable (` main.exe ` on Windows, ` main ` everywhere else)
34+ and make a new one.
35+ 3 . Make a Cargo configuration file.
36+
37+ Let's get started!
2438
25- Let’s convert Hello World to Cargo.
39+ ### Creating a new Executable and Source Directory
2640
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:
41+ First, go back to your terminal, move to your * hello_world* directory, and
42+ enter the following commands:
3043
3144``` bash
3245$ mkdir src
3346$ mv main.rs src/main.rs
3447$ rm main # or 'del main.exe' on Windows
3548```
3649
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.
50+ Cargo expects your source files to live inside a * src * directory, so do that
51+ first. This leaves the top level project directory (in this case,
52+ * hello_world * ) for READMEs, license information, and anything else not related
53+ to your code. In this way, using Cargo helps you keep your projects nice and
54+ tidy. There's a place for everything, and everything is in its place.
4255
43- [ crates-custom ] : http://doc.crates.io/manifest.html#configuring-a-target
56+ Now, copy * main.rs* to the * src* directory, and delete the compiled file you
57+ created with ` rustc ` . As usual, replace ` main ` with ` main.exe ` if you're on
58+ Windows.
4459
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.
60+ This example retains ` main.rs ` as the source filename because it's creating an
61+ executable. If you wanted to make a library instead, you'd name the file
62+ ` lib.rs ` . This convention is used by Cargo to successfully compile your
63+ projects, but it can be overridden if you wish.
4964
50- Next, our configuration file:
65+ ### Creating a Configuration File
5166
52- ``` bash
53- $ editor Cargo.toml # or 'notepad Cargo.toml' on Windows
54- ```
67+ Next, create a new file inside your * hello_world* directory, and call it
68+ ` Cargo.toml ` .
69+
70+ Make sure to capitalize the ` C ` in ` Cargo.toml ` , or Cargo won't know what to do
71+ with the configuration file.
5572
56- Make sure to get this name right: we need the capital ` C ` !
73+ This file is in the * [ TOML] * (Tom's Obvious, Minimal Language) format. TOML is
74+ similar to INI, but has some extra goodies. According to the TOML docs, TOML
75+ “aims to be a minimal configuration file format that's easy to read”, and so we
76+ chose it as the format Cargo uses.
5777
58- Put this inside:
78+ [ TOML ] : https://github.com/toml-lang/toml
79+
80+ Inside this file, type the following information:
5981
6082``` toml
6183[package ]
@@ -65,18 +87,21 @@ version = "0.0.1"
6587authors = [
" Your name <[email protected] >" ]
6688```
6789
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,
90+ The first line, ` [package] ` , indicates that the following statements are
91+ configuring a package. As we add more information to this file, we’ll add other
92+ sections, but for now, we just have the package configuration.
93+
94+ The other three lines set the three bits of configuration that Cargo needs to
95+ know to compile your program: its name, what version it is, and who wrote it.
7096
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.
97+ Once you've added this information to the * Cargo.toml* file, save it to finish
98+ creating the configuration file.
7599
76- [ toml ] : https://github.com/toml-lang/toml
100+ ## Building and Running a Cargo Project
77101
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:
102+ With your * Cargo.toml* file in place in your project's root directory, you
103+ should be ready to build and run your Hello World program! To do so, enter the
104+ following commands:
80105
81106``` bash
82107$ cargo build
@@ -85,18 +110,22 @@ $ ./target/debug/hello_world
85110Hello, world!
86111```
87112
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 ` :
113+ Bam! If all goes well, ` Hello, world! ` should print to the terminal once more.
114+
115+ You just built a project with ` cargo build ` and ran it with
116+ ` ./target/debug/hello_world ` , but you can actually do both in one step with
117+ ` cargo run ` as follows:
90118
91119``` bash
92120$ cargo run
93121 Running ` target/debug/hello_world`
94122Hello, world!
95123```
96124
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:
125+ Notice that this example didn’t re-build the project. Cargo figured out that
126+ the hasn’t changed, and so it just ran the binary. If you'd modified your
127+ program, Cargo would have built the file before running it, and you would have
128+ seen something like this:
100129
101130``` bash
102131$ cargo run
@@ -105,70 +134,71 @@ $ cargo run
105134Hello, world!
106135```
107136
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.
137+ Cargo checks to see if any of your project’s files have been modified, and only
138+ rebuilds your project if they’ve changed since the last time you built it.
139+
140+ With simple projects, Cargo doesn't bring a whole lot over just using ` rustc ` ,
141+ but it will become useful in future. When your projects get more complex,
142+ you'll need to do more things to get all of the parts to properly compile. With
143+ Cargo, you can just run ` cargo build ` , and it should work the right way.
144+
145+ ## Building for Release
112146
113- When our project is finally ready for release, we can use `cargo build
114- --release` to compile our project with optimizations.
147+ When your project is finally ready for release, you can use `cargo build
148+ --release` to compile your project with optimizations. These optimizations make
149+ your Rust code run faster, but turning them on makes your program take longer
150+ to compile. This is why there are two different profiles, one for development,
151+ and one for building the final program you’ll give to a user.
115152
116- You'll also notice that Cargo has created a new file: ` Cargo.lock ` .
153+ Running this command also causes Cargo to create a new file called
154+ * Cargo.lock* , which looks like this:
117155
118156``` toml
119157[root ]
120158name = " hello_world"
121159version = " 0.0.1"
122160```
123161
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.
162+ Cargo uses the * Cargo.lock* file to keep track of dependencies in your
163+ application. This is the Hello World project's * Cargo.lock* file. This project
164+ doesn't have dependencies, so the file is a bit sparse. Realistically, you
165+ won't ever need to touch this file yourself; just let Cargo handle it.
127166
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
131- all Rust projects:
167+ That’s it! If you've been following along, you should have successfully built
168+ ` hello_world ` with Cargo.
169+
170+ Even though the project is simple, it now uses much of the real tooling you’ll
171+ use for the rest of your Rust career. In fact, you can expect to start
172+ virtually all Rust projects with some variation on the following commands:
132173
133174``` bash
134175$ git clone someurl.com/foo
135176$ cd foo
136177$ cargo build
137178```
138179
139- ## A New Project
180+ ## Making A New Cargo Project the Easy Way
140181
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.
182+ You don’t have to go through that previous process every time you want to start
183+ a new project! Cargo can quickly make a bare-bones project directory that you
184+ can start developing in right away.
144185
145- To start a new project with Cargo, we use ` cargo new ` :
186+ To start a new project with Cargo, enter ` cargo new ` at the command line :
146187
147188``` bash
148189$ cargo new hello_world --bin
149190```
150191
151- We’re passing ` --bin ` because our goal is to get straight to making an
192+ This command passes ` --bin ` because the goal is to get straight to making an
152193executable application, as opposed to a library. Executables are often called
153- ‘binaries.’ (as in ` /usr/bin ` , if we’re on a Unix system)
154-
155- Let's check out what Cargo has generated for us:
194+ * binaries* (as in ` /usr/bin ` , if you’re on a Unix system).
156195
157- ``` bash
158- $ cd hello_world
159- $ tree .
160- .
161- ├── Cargo.toml
162- └── src
163- └── main.rs
164-
165- 1 directory, 2 files
166- ```
167-
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.
196+ Cargo has generated two files and one directory for us: a ` Cargo.toml ` and a
197+ * src* directory with a * main.rs* file inside. These should look familliar,
198+ they’re exactly what we created by hand, above.
170199
171- This is all we need to get started. First, let’s check out ` Cargo.toml ` :
200+ This output is all you need to get started. First, open ` Cargo.toml ` . It should
201+ look something like this:
172202
173203``` toml
174204[package ]
@@ -178,33 +208,36 @@ version = "0.1.0"
178208authors = [
" Your Name <[email protected] >" ]
179209```
180210
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
211+ Cargo has populated * Cargo.toml * with reasonable defaults based on the arguments
212+ you gave it and your ` git ` global configuration. You may notice that Cargo has
183213also initialized the ` hello_world ` directory as a ` git ` repository.
184214
185- Here’s what’s in ` src/main.rs ` :
215+ Here’s what should be in ` src/main.rs ` :
186216
187217``` rust
188218fn main () {
189219 println! (" Hello, world!" );
190220}
191221```
192222
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
195- depth.
223+ Cargo has generated a "Hello World!" for you, and you’re ready to start coding!
224+
225+ > Note: If you want to look at Cargo in more detail, check out the official [ Cargo
226+ guide] , which covers all of its features.
227+
228+ [ Cargo guide ] : http://doc.crates.io/guide.html
196229
197- [ guide ] : http://doc.crates.io/guide.html
230+ # Closing Thoughts
198231
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.
232+ This chapter covered the basics that will serve you well through the rest of
233+ this book, and the rest of your time with Rust. Now that you’ve got the tools
234+ down, we'll cover more about the Rust language itself.
202235
203236You 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.
237+ start from the bottom and work your way up with ‘ [ Syntax and
238+ Semantics] [ syntax ] ’. More experienced systems programmers will probably prefer
239+ ‘Learn Rust’, while those from dynamic backgrounds may enjoy either. Different
240+ people learn differently! Choose whatever’s right for you.
208241
209242[ learnrust ] : learn-rust.html
210243[ syntax ] : syntax-and-semantics.html
0 commit comments