Skip to content

Guide: cargo new #16186

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 30 additions & 53 deletions src/doc/guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ Put this inside:

name = "hello_world"
version = "0.1.0"
authors = [ "someone@example.com" ]
authors = [ "Your name <you@example.com>" ]

[[bin]]

Expand Down Expand Up @@ -354,6 +354,18 @@ file, we would need to call `rustc` twice, and pass it a bunch of options to
tell it to build everything together. With Cargo, as our project grows, we can
just `cargo build` and it'll work the right way.

You'll also notice that Cargo has created a new file: `Cargo.lock`.

```{ignore,notrust}
[root]
name = "hello_world"
version = "0.0.1"
```

This file is used by Cargo to keep track of dependencies in your application.
Right now, we don't have any, so it's a bit sparse. You won't ever need
to touch this file yourself, just let Cargo handle it.

That's it! We've successfully built `hello_world` with Cargo. Even though our
program is simple, it's using much of the real tooling that you'll use for the
rest of your Rust career.
Expand Down Expand Up @@ -1594,41 +1606,45 @@ taken to the screen. Sound good?

## Set up

Let's set up a new project. Go to your projects directory, and make a new
directory for the project, as well as a `src` directory for our code:
Let's set up a new project. Go to your projects directory. Remember how we
had to create our directory structure and a `Cargo.toml` for `hello_world`? Cargo
has a command that does that for us. Let's give it a shot:

```{bash}
$ cd ~/projects
$ mkdir guessing_game
$ cargo new guessing_game --bin
$ cd guessing_game
$ mkdir src
```

Great. Next, let's make a `Cargo.toml` file so Cargo knows how to build our
project:
We pass the name of our project to `cargo new`, and then the `--bin` flag,
since we're making a binary, rather than a library.

Check out the generated `Cargo.toml`:

```{ignore}
[package]

name = "guessing_game"
version = "0.1.0"
authors = [ "someone@example.com" ]
authors = ["Your Name <you@example.com>"]

[[bin]]

name = "guessing_game"
```

Finally, we need our source file. Let's just make it hello world for now, so we
can check that our setup works. In `src/guessing_game.rs`:
Cargo gets this information from your environment. If it's not correct, go ahead
and fix that.

Finally, Cargo generated a hello, world for us. Check out `src/main.rs`:

```{rust}
fn main() {
println!("Hello world!");
}
```

Let's make sure that worked:
Let's try compiling what Cargo gave us:

```{bash}
$ cargo build
Expand Down Expand Up @@ -1883,7 +1899,6 @@ fn cmp(a: int, b: int) -> Ordering {
If we try to compile, we'll get some errors:

```{notrust,ignore}
$ cargo build
$ cargo build
Compiling guessing_game v0.1.0 (file:/home/you/projects/guessing_game)
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)
Expand Down Expand Up @@ -2486,27 +2501,7 @@ Enough talk, let's build something! Let's make a new project called `modules`.

```{bash,ignore}
$ cd ~/projects
$ mkdir modules
$ cd modules
$ mkdir src
```

We need to make our two 'hello world' files. In `src/main.rs`:

```{rust}
fn main() {
println!("Hello, world!");
}
```

And in `Cargo.toml`:

```{notrust,ignore}
[package]

name = "modules"
version = "0.1.0"
authors = [ "[email protected]" ]
$ cargo new modules --bin
```

Let's double check our work by compiling:
Expand Down Expand Up @@ -2924,34 +2919,16 @@ now: make a new project:

```{bash,ignore}
$ cd ~/projects
$ mkdir testing
$ cargo new testing --bin
$ cd testing
$ mkdir test
```

In `src/main.rs`:

```{rust}
fn main() {
println!("Hello, world!");
}
```

And in `Cargo.toml`:

```{notrust,ignore}
[package]

name = "testing"
version = "0.1.0"
authors = [ "[email protected]" ]
```

And try it out:

```{notrust,ignore}
$ cargo run
Compiling testing v0.1.0 (file:/home/you/projects/testing)
Running `target/testing`
Hello, world!
$
```
Expand Down