Skip to content

Commit e87cc59

Browse files
committed
Rollup merge of rust-lang#22732 - jxcl:cargo-new, r=steveklabnik
r? @steveklabnik
2 parents 3a96d6a + f49fd40 commit e87cc59

File tree

1 file changed

+62
-4
lines changed

1 file changed

+62
-4
lines changed

src/doc/trpl/hello-cargo.md

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@ the Cargo
1818
README](https://github.com/rust-lang/cargo#installing-cargo-from-nightlies)
1919
for specific instructions about installing it.
2020

21+
## Converting to Cargo
22+
2123
Let's convert Hello World to Cargo.
2224

2325
To Cargo-ify our project, we need to do two things: Make a `Cargo.toml`
2426
configuration file, and put our source file in the right place. Let's
2527
do that part first:
2628

27-
```{bash}
29+
```bash
2830
$ mkdir src
2931
$ mv main.rs src/main.rs
3032
```
@@ -36,7 +38,7 @@ place for everything, and everything in its place.
3638

3739
Next, our configuration file:
3840

39-
```{bash}
41+
```bash
4042
$ editor Cargo.toml
4143
```
4244

@@ -73,7 +75,7 @@ well as what it is named.
7375

7476
Once you have this file in place, we should be ready to build! Try this:
7577

76-
```{bash}
78+
```bash
7779
$ cargo build
7880
Compiling hello_world v0.0.1 (file:///home/yourname/projects/hello_world)
7981
$ ./target/hello_world
@@ -103,6 +105,62 @@ That's it! We've successfully built `hello_world` with Cargo. Even though our
103105
program is simple, it's using much of the real tooling that you'll use for the
104106
rest of your Rust career.
105107

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+
106164
Now that you've got the tools down, let's actually learn more about the Rust
107165
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

Comments
 (0)