@@ -264,26 +264,139 @@ projects.
264
264
265
265
## Hello, Cargo!
266
266
267
+ [ Cargo] ( http://crates.io ) is a tool that Rustaceans use to help manage their
268
+ Rust projects. Cargo is currently in an alpha state, just like Rust, and so it
269
+ is still a work in progress. However, it is already good enough to use for many
270
+ Rust projects, and so it is assumed that Rust projects will use Cargo from the
271
+ beginning.
267
272
273
+ Programmers love car analogies, so I've got a good one for you to think about
274
+ the relationship between ` cargo ` and ` rustc ` : ` rustc ` is like a car, and
275
+ ` cargo ` is like a robotic driver. You can drive your car yourself, of course,
276
+ but isn't it just easier to let a computer drive it for you?
268
277
278
+ Anyway, Cargo manages three things: building your code, downloading the
279
+ dependencies your code needs, and building the dependencies your code needs.
280
+ At first, your program doesn't have any dependencies, so we'll only be using
281
+ the first part of its functionality. Eventually, we'll add more. Since we
282
+ started off by using Cargo, it'll be easy to add later.
269
283
284
+ Let's convert Hello World to Cargo. The first thing we need to do is install
285
+ it. To do this, we need to build it from source. There are no binaries yet.
270
286
287
+ First, let's go back to our projects directory. We don't want Cargo to
288
+ live in our project!
271
289
290
+ ``` {bash}
291
+ $ cd ..
292
+ ```
293
+
294
+ Next, we need these commands:
295
+
296
+ ``` {bash}
297
+ $ git clone --recursive https://github.com/rust-lang/cargo
298
+ $ cd cargo
299
+ $ make
300
+ $ make install # may need sudo or admin permissions
301
+ ```
302
+
303
+ The ` --recursive ` downloads Cargo's own dependencies. You can't use Cargo to
304
+ fetch dependencies until you have Cargo installed!
305
+
306
+ Let's see if that worked. Try this:
307
+
308
+ ``` {bash}
309
+ $ cargo
310
+ Commands:
311
+ build # compile the current project
312
+
313
+ Options (for all commands):
314
+
315
+ -v, [--verbose]
316
+ -h, [--help]
317
+ ```
318
+
319
+ If you see this output when you run ` cargo ` , congrats! Cargo is working. If
320
+ not, please [ open an Issue] ( https://github.com/rust-lang/cargo/issues/new ) or
321
+ drop by the Rust IRC, and we can help you out.
272
322
323
+ Let's move back into our ` hello_world ` directory now:
273
324
325
+ ``` {bash}
326
+ $ cd .. # move back up into projects
327
+ $ cd hello_world # move into hello_world
328
+ ```
329
+
330
+ To Cargo-ify our project, we need to do two things: Make a ` Cargo.toml `
331
+ configuration file, and put our source file in the right place. Let's
332
+ do that part first:
333
+
334
+ ``` {bash}
335
+ $ mkdir src
336
+ $ mv hello_world.rs src/hello_world.rs
337
+ ```
338
+
339
+ Cargo expects your source files to live inside a ` src ` directory. That leaves
340
+ the top level for other things, like READMEs, licence information, and anything
341
+ not related to your code. Cargo helps us keep our projects nice and tidy. A
342
+ place for everything, and everything in its place.
274
343
344
+ Next, our configuration file:
275
345
346
+ ``` {bash}
347
+ $ editor Cargo.toml
348
+ ```
276
349
350
+ Make sure to get this name right: you need the capital ` C ` !
277
351
352
+ Put this inside:
278
353
354
+ ```
355
+ [package]
279
356
357
+ name = "hello_world"
358
+ version = "0.1.0"
359
+ authors = [ "someone@example.com" ]
280
360
361
+ [[bin]]
281
362
363
+ name = "hello_world"
364
+ ```
282
365
366
+ This file is in the [ TOML] ( https://github.com/toml-lang/toml ) format. Let's let
367
+ it explain itself to you:
283
368
369
+ > TOML aims to be a minimal configuration file format that's easy to read due
370
+ > to obvious semantics. TOML is designed to map unambiguously to a hash table.
371
+ > TOML should be easy to parse into data structures in a wide variety of
372
+ > languages.
284
373
374
+ TOML is very similar to INI, but with some extra goodies.
285
375
376
+ Anyway, there are two ** table** s in this file: ` package ` and ` bin ` . The first
377
+ tells Cargo metadata about your package. The second tells Cargo that we're
378
+ interested in building a binary, not a library (though we could do both!), as
379
+ well as what it is named.
286
380
381
+ Once you have this file in place, we should be ready to build! Try this:
382
+
383
+ ``` {bash}
384
+ $ cargo build
385
+ Compiling hello_world v0.1.0 (file:/home/yourname/projects/hello_world)
386
+ $ ./target/hello_world
387
+ Hello, world!
388
+ ```
287
389
390
+ Bam! We build our project with ` cargo build ` , and run it with
391
+ ` ./target/hello_world ` . This hasn't bought us a whole lot over our simple use
392
+ of ` rustc ` , but think about the future: when our project has more tha one file,
393
+ we would need to call ` rustc ` twice, and pass it a bunch of options to tell it
394
+ to build everything together. With Cargo, as our project grows, we can just
395
+ ` cargo build ` and it'll work the right way.
288
396
397
+ That's it! We've successfully built ` hello_world ` with Cargo. Even though our
398
+ program is simple, it's using all of the real tooling that you'll use for the
399
+ rest of your Rust career.
289
400
401
+ Next, we'll learn more about Rust itself, by starting to write a more complicated
402
+ program. We hope you want to do more with Rust than just print "Hello, world!"
0 commit comments