@@ -18,13 +18,15 @@ the Cargo
18
18
README] ( https://github.com/rust-lang/cargo#installing-cargo-from-nightlies )
19
19
for specific instructions about installing it.
20
20
21
+ ## Converting to Cargo
22
+
21
23
Let's convert Hello World to Cargo.
22
24
23
25
To Cargo-ify our project, we need to do two things: Make a ` Cargo.toml `
24
26
configuration file, and put our source file in the right place. Let's
25
27
do that part first:
26
28
27
- ``` { bash}
29
+ ``` bash
28
30
$ mkdir src
29
31
$ mv main.rs src/main.rs
30
32
```
@@ -36,7 +38,7 @@ place for everything, and everything in its place.
36
38
37
39
Next, our configuration file:
38
40
39
- ``` { bash}
41
+ ``` bash
40
42
$ editor Cargo.toml
41
43
```
42
44
@@ -73,7 +75,7 @@ well as what it is named.
73
75
74
76
Once you have this file in place, we should be ready to build! Try this:
75
77
76
- ``` { bash}
78
+ ``` bash
77
79
$ cargo build
78
80
Compiling hello_world v0.0.1 (file:///home/yourname/projects/hello_world)
79
81
$ ./target/hello_world
@@ -103,6 +105,62 @@ That's it! We've successfully built `hello_world` with Cargo. Even though our
103
105
program is simple, it's using much of the real tooling that you'll use for the
104
106
rest of your Rust career.
105
107
108
+ ## A New Project
109
+
110
+ You don't have to go through this whole process every time you want to start a new
111
+ project! Cargo has the ability to make a bare-bones project directory in which you
112
+ can start developing right away.
113
+
114
+ To start a new project with Cargo, use ` cargo new ` :
115
+
116
+ ``` bash
117
+ $ cargo new hello_world --bin
118
+ ```
119
+
120
+ We're passing ` --bin ` because we're making a binary program: if we
121
+ were making a library, we'd leave it off.
122
+
123
+ Let's check out what Cargo has generated for us:
124
+
125
+ ``` bash
126
+ $ cd hello_world
127
+ $ tree .
128
+ .
129
+ ├── Cargo.toml
130
+ └── src
131
+ └── main.rs
132
+
133
+ 1 directory, 2 files
134
+ ```
135
+
136
+ If you don't have the ` tree ` command, you can probably get it from your distro's package
137
+ manager. It's not necessary, but it's certainly useful.
138
+
139
+ This is all we need to get started. First, let's check out ` Cargo.toml ` :
140
+
141
+ ``` toml
142
+ [package ]
143
+
144
+ name = " hello_world"
145
+ version = " 0.0.1"
146
+ authors = [" Your Name <you@example.com>" ]
147
+ ```
148
+
149
+ Cargo has populated this file with reasonable defaults based off the arguments you gave
150
+ it and your ` git ` global configuration. You may notice that Cargo has also initialized
151
+ the ` hello_world ` directory as a ` git ` repository.
152
+
153
+ Here's what's in ` src/main.rs ` :
154
+
155
+ ``` rust
156
+ fn main () {
157
+ println! (" Hello, world!" );
158
+ }
159
+ ```
160
+
161
+ Cargo has generated a "Hello World!" for us, and you're ready to start coding! A
162
+ much more in-depth guide to Cargo can be found [ here] ( http://doc.crates.io/guide.html ) .
163
+
106
164
Now that you've got the tools down, let's actually learn more about the Rust
107
165
language itself. These are the basics that will serve you well through the rest
108
- of your time with Rust.
166
+ of your time with Rust.
0 commit comments