@@ -316,7 +316,7 @@ Put this inside:
316
316
317
317
name = "hello_world"
318
318
version = "0.1.0"
319
- authors = [ "someone @example.com" ]
319
+ authors = [ "Your name <you @example.com> " ]
320
320
321
321
[[bin]]
322
322
@@ -354,6 +354,18 @@ file, we would need to call `rustc` twice, and pass it a bunch of options to
354
354
tell it to build everything together. With Cargo, as our project grows, we can
355
355
just ` cargo build ` and it'll work the right way.
356
356
357
+ You'll also notice that Cargo has created a new file: ` Cargo.lock ` .
358
+
359
+ ``` {ignore,notrust}
360
+ [root]
361
+ name = "hello_world"
362
+ version = "0.0.1"
363
+ ```
364
+
365
+ This file is used by Cargo to keep track of dependencies in your application.
366
+ Right now, we don't have any, so it's a bit sparse. You won't ever need
367
+ to touch this file yourself, just let Cargo handle it.
368
+
357
369
That's it! We've successfully built ` hello_world ` with Cargo. Even though our
358
370
program is simple, it's using much of the real tooling that you'll use for the
359
371
rest of your Rust career.
@@ -1594,41 +1606,45 @@ taken to the screen. Sound good?
1594
1606
1595
1607
## Set up
1596
1608
1597
- Let's set up a new project. Go to your projects directory, and make a new
1598
- directory for the project, as well as a ` src ` directory for our code:
1609
+ Let's set up a new project. Go to your projects directory. Remember how we
1610
+ had to create our directory structure and a ` Cargo.toml ` for ` hello_world ` ? Cargo
1611
+ has a command that does that for us. Let's give it a shot:
1599
1612
1600
1613
``` {bash}
1601
1614
$ cd ~/projects
1602
- $ mkdir guessing_game
1615
+ $ cargo new guessing_game --bin
1603
1616
$ cd guessing_game
1604
- $ mkdir src
1605
1617
```
1606
1618
1607
- Great. Next, let's make a ` Cargo.toml ` file so Cargo knows how to build our
1608
- project:
1619
+ We pass the name of our project to ` cargo new ` , and then the ` --bin ` flag,
1620
+ since we're making a binary, rather than a library.
1621
+
1622
+ Check out the generated ` Cargo.toml ` :
1609
1623
1610
1624
``` {ignore}
1611
1625
[package]
1612
1626
1613
1627
name = "guessing_game"
1614
1628
version = "0.1.0"
1615
- authors = [ "someone @example.com" ]
1629
+ authors = ["Your Name <you @example.com>" ]
1616
1630
1617
1631
[[bin]]
1618
1632
1619
1633
name = "guessing_game"
1620
1634
```
1621
1635
1622
- Finally, we need our source file. Let's just make it hello world for now, so we
1623
- can check that our setup works. In ` src/guessing_game.rs ` :
1636
+ Cargo gets this information from your environment. If it's not correct, go ahead
1637
+ and fix that.
1638
+
1639
+ Finally, Cargo generated a hello, world for us. Check out ` src/main.rs ` :
1624
1640
1625
1641
``` {rust}
1626
1642
fn main() {
1627
1643
println!("Hello world!");
1628
1644
}
1629
1645
```
1630
1646
1631
- Let's make sure that worked :
1647
+ Let's try compiling what Cargo gave us :
1632
1648
1633
1649
``` {bash}
1634
1650
$ cargo build
@@ -1883,7 +1899,6 @@ fn cmp(a: int, b: int) -> Ordering {
1883
1899
If we try to compile, we'll get some errors:
1884
1900
1885
1901
``` {notrust,ignore}
1886
- $ cargo build
1887
1902
$ cargo build
1888
1903
Compiling guessing_game v0.1.0 (file:/home/you/projects/guessing_game)
1889
1904
src/guessing_game.rs:20:15: 20:20 error: mismatched types: expected `int` but found `collections::string::String` (expected int but found struct collections::string::String)
@@ -2486,27 +2501,7 @@ Enough talk, let's build something! Let's make a new project called `modules`.
2486
2501
2487
2502
``` {bash,ignore}
2488
2503
$ cd ~/projects
2489
- $ mkdir modules
2490
- $ cd modules
2491
- $ mkdir src
2492
- ```
2493
-
2494
- We need to make our two 'hello world' files. In ` src/main.rs ` :
2495
-
2496
- ``` {rust}
2497
- fn main() {
2498
- println!("Hello, world!");
2499
- }
2500
- ```
2501
-
2502
- And in ` Cargo.toml ` :
2503
-
2504
- ``` {notrust,ignore}
2505
- [package]
2506
-
2507
- name = "modules"
2508
- version = "0.1.0"
2509
- authors = [ "someone@example.com" ]
2504
+ $ cargo new modules --bin
2510
2505
```
2511
2506
2512
2507
Let's double check our work by compiling:
@@ -2924,34 +2919,16 @@ now: make a new project:
2924
2919
2925
2920
``` {bash,ignore}
2926
2921
$ cd ~/projects
2927
- $ mkdir testing
2922
+ $ cargo new testing --bin
2928
2923
$ cd testing
2929
- $ mkdir test
2930
- ```
2931
-
2932
- In ` src/main.rs ` :
2933
-
2934
- ``` {rust}
2935
- fn main() {
2936
- println!("Hello, world!");
2937
- }
2938
- ```
2939
-
2940
- And in ` Cargo.toml ` :
2941
-
2942
- ``` {notrust,ignore}
2943
- [package]
2944
-
2945
- name = "testing"
2946
- version = "0.1.0"
2947
- authors = [ "someone@example.com" ]
2948
2924
```
2949
2925
2950
2926
And try it out:
2951
2927
2952
2928
``` {notrust,ignore}
2953
2929
$ cargo run
2954
2930
Compiling testing v0.1.0 (file:/home/you/projects/testing)
2931
+ Running `target/testing`
2955
2932
Hello, world!
2956
2933
$
2957
2934
```
0 commit comments