]>
Commit | Line | Data |
---|---|---|
1a4d82fc JJ |
1 | % Hello, Cargo! |
2 | ||
9346a6ac AL |
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. | |
7 | ||
bd371182 | 8 | [cratesio]: http://doc.crates.io |
1a4d82fc | 9 | |
b039eaaf SL |
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. | |
1a4d82fc | 15 | |
b039eaaf SL |
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. | |
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 | 25 | Let’s convert Hello World to Cargo. |
1a4d82fc | 26 | |
c1a9b12d SL |
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: | |
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 |
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. | |
1a4d82fc JJ |
49 | |
50 | Next, our configuration file: | |
51 | ||
c34b1796 | 52 | ```bash |
b039eaaf | 53 | $ editor Cargo.toml # or 'notepad Cargo.toml' on Windows |
1a4d82fc JJ |
54 | ``` |
55 | ||
b039eaaf | 56 | Make sure to get this name right: we need the capital `C`! |
1a4d82fc JJ |
57 | |
58 | Put this inside: | |
59 | ||
60 | ```toml | |
61 | [package] | |
62 | ||
63 | name = "hello_world" | |
64 | version = "0.0.1" | |
65 | authors = [ "Your name <you@example.com>" ] | |
1a4d82fc JJ |
66 | ``` |
67 | ||
c1a9b12d SL |
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, | |
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 |
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: | |
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 |
85 | Hello, world! |
86 | ``` | |
87 | ||
62682a34 | 88 | Bam! 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` | |
94 | Hello, world! | |
95 | ``` | |
96 | ||
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: | |
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` | |
105 | Hello, world! | |
106 | ``` | |
107 | ||
108 | This hasn’t bought us a whole lot over our simple use of `rustc`, but think | |
62682a34 | 109 | about the future: when our project gets more complex, we need to do more |
9346a6ac | 110 | things to get all of the parts to properly compile. With Cargo, as our project |
62682a34 | 111 | grows, we can just run `cargo build`, and it’ll work the right way. |
9346a6ac | 112 | |
b039eaaf SL |
113 | When our project is finally ready for release, we can use `cargo build |
114 | --release` to compile our project with optimizations. | |
1a4d82fc JJ |
115 | |
116 | You'll also notice that Cargo has created a new file: `Cargo.lock`. | |
117 | ||
118 | ```toml | |
119 | [root] | |
120 | name = "hello_world" | |
121 | version = "0.0.1" | |
122 | ``` | |
123 | ||
b039eaaf SL |
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. | |
1a4d82fc | 127 | |
9346a6ac | 128 | That’s it! We’ve successfully built `hello_world` with Cargo. Even though our |
b039eaaf SL |
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: | |
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 |
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. | |
c34b1796 | 144 | |
b039eaaf | 145 | To 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 |
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) | |
c34b1796 AL |
154 | |
155 | Let'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 | ||
165 | 1 directory, 2 files | |
166 | ``` | |
167 | ||
b039eaaf | 168 | If we don't have the `tree` command, we can probably get it from our |
9346a6ac | 169 | distribution’s package manager. It’s not necessary, but it’s certainly useful. |
c34b1796 | 170 | |
9346a6ac | 171 | This is all we need to get started. First, let’s check out `Cargo.toml`: |
c34b1796 AL |
172 | |
173 | ```toml | |
174 | [package] | |
175 | ||
176 | name = "hello_world" | |
62682a34 | 177 | version = "0.1.0" |
c34b1796 AL |
178 | authors = ["Your Name <you@example.com>"] |
179 | ``` | |
180 | ||
9346a6ac | 181 | Cargo has populated this file with reasonable defaults based off the arguments |
b039eaaf | 182 | we gave it and our `git` global configuration. You may notice that Cargo has |
9346a6ac | 183 | also initialized the `hello_world` directory as a `git` repository. |
c34b1796 | 184 | |
9346a6ac | 185 | Here’s what’s in `src/main.rs`: |
c34b1796 AL |
186 | |
187 | ```rust | |
188 | fn main() { | |
189 | println!("Hello, world!"); | |
190 | } | |
191 | ``` | |
192 | ||
b039eaaf SL |
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. | |
c34b1796 | 196 | |
9346a6ac AL |
197 | [guide]: http://doc.crates.io/guide.html |
198 | ||
b039eaaf SL |
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. | |
9346a6ac AL |
202 | |
203 | You have two options: Dive into a project with ‘[Learn Rust][learnrust]’, or | |
b039eaaf SL |
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. | |
9346a6ac AL |
208 | |
209 | [learnrust]: learn-rust.html | |
210 | [syntax]: syntax-and-semantics.html |