Use cargo new.
Now that this feature exists, we should use it. Fixes #16078
This commit is contained in:
parent
d7cfc34a22
commit
1a80dcbd56
1 changed files with 18 additions and 53 deletions
|
@ -316,7 +316,7 @@ Put this inside:
|
||||||
|
|
||||||
name = "hello_world"
|
name = "hello_world"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
authors = [ "someone@example.com" ]
|
authors = [ "Your name <you@example.com>" ]
|
||||||
|
|
||||||
[[bin]]
|
[[bin]]
|
||||||
|
|
||||||
|
@ -1594,33 +1594,37 @@ taken to the screen. Sound good?
|
||||||
|
|
||||||
## Set up
|
## Set up
|
||||||
|
|
||||||
Let's set up a new project. Go to your projects directory, and make a new
|
Let's set up a new project. Go to your projects directory. Remember how we
|
||||||
directory for the project, as well as a `src` directory for our code:
|
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}
|
```{bash}
|
||||||
$ cd ~/projects
|
$ cd ~/projects
|
||||||
$ mkdir guessing_game
|
$ cargo new guessing_game --bin
|
||||||
$ cd guessing_game
|
$ cd guessing_game
|
||||||
$ mkdir src
|
|
||||||
```
|
```
|
||||||
|
|
||||||
Great. Next, let's make a `Cargo.toml` file so Cargo knows how to build our
|
We pass the name of our project to `cargo new`, and then the `--bin` flag,
|
||||||
project:
|
since we're making a binary, rather than a library.
|
||||||
|
|
||||||
|
Check out the generated `Cargo.toml`:
|
||||||
|
|
||||||
```{ignore}
|
```{ignore}
|
||||||
[package]
|
[package]
|
||||||
|
|
||||||
name = "guessing_game"
|
name = "guessing_game"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
authors = [ "someone@example.com" ]
|
authors = ["Your Name <you@example.com>"]
|
||||||
|
|
||||||
[[bin]]
|
[[bin]]
|
||||||
|
|
||||||
name = "guessing_game"
|
name = "guessing_game"
|
||||||
```
|
```
|
||||||
|
|
||||||
Finally, we need our source file. Let's just make it hello world for now, so we
|
Cargo gets this information from your environment. If it's not correct, go ahead
|
||||||
can check that our setup works. In `src/guessing_game.rs`:
|
and fix that.
|
||||||
|
|
||||||
|
Finally, Cargo generated a hello, world for us. Check out `src/main.rs`:
|
||||||
|
|
||||||
```{rust}
|
```{rust}
|
||||||
fn main() {
|
fn main() {
|
||||||
|
@ -1628,7 +1632,7 @@ fn main() {
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Let's make sure that worked:
|
Let's try compiling what Cargo gave us:
|
||||||
|
|
||||||
```{bash}
|
```{bash}
|
||||||
$ cargo build
|
$ cargo build
|
||||||
|
@ -1883,7 +1887,6 @@ fn cmp(a: int, b: int) -> Ordering {
|
||||||
If we try to compile, we'll get some errors:
|
If we try to compile, we'll get some errors:
|
||||||
|
|
||||||
```{notrust,ignore}
|
```{notrust,ignore}
|
||||||
$ cargo build
|
|
||||||
$ cargo build
|
$ cargo build
|
||||||
Compiling guessing_game v0.1.0 (file:/home/you/projects/guessing_game)
|
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)
|
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 +2489,7 @@ Enough talk, let's build something! Let's make a new project called `modules`.
|
||||||
|
|
||||||
```{bash,ignore}
|
```{bash,ignore}
|
||||||
$ cd ~/projects
|
$ cd ~/projects
|
||||||
$ mkdir modules
|
$ cargo new modules --bin
|
||||||
$ 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 = [ "someone@example.com" ]
|
|
||||||
```
|
```
|
||||||
|
|
||||||
Let's double check our work by compiling:
|
Let's double check our work by compiling:
|
||||||
|
@ -2924,27 +2907,8 @@ now: make a new project:
|
||||||
|
|
||||||
```{bash,ignore}
|
```{bash,ignore}
|
||||||
$ cd ~/projects
|
$ cd ~/projects
|
||||||
$ mkdir testing
|
$ cargo new testing --bin
|
||||||
$ cd testing
|
$ 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 = [ "someone@example.com" ]
|
|
||||||
```
|
```
|
||||||
|
|
||||||
And try it out:
|
And try it out:
|
||||||
|
@ -2952,6 +2916,7 @@ And try it out:
|
||||||
```{notrust,ignore}
|
```{notrust,ignore}
|
||||||
$ cargo run
|
$ cargo run
|
||||||
Compiling testing v0.1.0 (file:/home/you/projects/testing)
|
Compiling testing v0.1.0 (file:/home/you/projects/testing)
|
||||||
|
Running `target/testing`
|
||||||
Hello, world!
|
Hello, world!
|
||||||
$
|
$
|
||||||
```
|
```
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue