Skip to content

Commit 97452de

Browse files
committed
Auto merge of #4453 - behnam:book, r=alexcrichton
Import mdBook-based docs and sync Since we decided to keep the mdBook-based docs in-repo, I have imported the existing converted docs from <https://github.com/istankovic/cargo-book> under `/src/doc/book/` here, and have synced the current docs and the mdBook-based ones manually, file-by-file. I have created a `MIGRATION_MAP` file, which shows the relationship between old docs and new ones. The first column is the old file, the second column is the canonical location in mdBook, and the rest of columns are globs of other files in mdBook containing content from the old file. The first and second column of `MIGRATION_MAP` shall later be used to create redirect rules from `doc.crates.io/` to `doc.rust-lang.org/cargo/`. I will also add a README file to remind everyone to keep these files in sync during the migration. There are also two or three small wording fixes here, which I'll note inline. This is a retry of <#4220>. First step for <#4040>.
2 parents f519d3d + bd5ecd4 commit 97452de

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+3686
-80
lines changed

src/doc/MIGRATION_MAP

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
build-script.md book/src/03-05-build-scripts.md
2+
config.md book/src/03-03-config.md
3+
crates-io.md book/src/03-06-crates-io.md
4+
environment-variables.md book/src/03-04-environment-variables.md
5+
external-tools.md book/src/03-09-external-tools.md
6+
faq.md book/src/faq.md
7+
guide.md book/src/guide.md book/src/02-*.md
8+
index.md book/src/SUMMARY.md book/src/01-*.md
9+
manifest.md book/src/03-02-manifest.md
10+
pkgid-spec.md book/src/03-07-pkgid-spec.md
11+
policies.md book/src/03-10-policies.md
12+
source-replacement.md book/src/03-08-source-replacement.md
13+
specifying-dependencies.md book/src/03-01-specifying-dependencies.md

src/doc/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Cargo Documentation
2+
3+
NOTE: Cargo documentation is under migration to mdBook-based structure. All the
4+
`*.md` files here shall be kept in sync with the `*.md` files under `book/src/`.
5+
See `MIGRATION_MAP` file here and <https://github.com/rust-lang/cargo/pull/4453>
6+
for details.

src/doc/book/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/book

src/doc/book/README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# The Cargo Book
2+
3+
4+
### Requirements
5+
6+
Building the book requires [mdBook]. To get it:
7+
8+
[mdBook]: https://github.com/azerupi/mdBook
9+
10+
```bash
11+
$ cargo install mdbook
12+
```
13+
14+
### Building
15+
16+
To build the book:
17+
18+
```bash
19+
$ mdbook build
20+
```
21+
22+
The output will be in the `book` subdirectory. To check it out, open it in
23+
your web browser.
24+
25+
_Firefox:_
26+
```bash
27+
$ firefox book/index.html # Linux
28+
$ open -a "Firefox" book/index.html # OS X
29+
$ Start-Process "firefox.exe" .\book\index.html # Windows (PowerShell)
30+
$ start firefox.exe .\book\index.html # Windows (Cmd)
31+
```
32+
33+
_Chrome:_
34+
```bash
35+
$ google-chrome book/index.html # Linux
36+
$ open -a "Google Chrome" book/index.html # OS X
37+
$ Start-Process "chrome.exe" .\book\index.html # Windows (PowerShell)
38+
$ start chrome.exe .\book\index.html # Windows (Cmd)
39+
```
40+
41+
42+
## Contributing
43+
44+
Given that the book is still in a draft state, we'd love your help! Please feel free to open
45+
issues about anything, and send in PRs for things you'd like to fix or change. If your change is
46+
large, please open an issue first, so we can make sure that it's something we'd accept before you
47+
go through the work of getting a PR together.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
## Installation
2+
3+
The easiest way to get Cargo is to get the current stable release of Rust by
4+
using the `rustup` script:
5+
6+
```shell
7+
$ curl -sSf https://static.rust-lang.org/rustup.sh | sh
8+
```
9+
10+
This will get you the current stable release of Rust for your platform along
11+
with the latest Cargo.
12+
13+
If you are on Windows, you can directly download the latest stable Rust and nightly Cargo:
14+
15+
- [Rust (32-bit)](https://static.rust-lang.org/dist/rust-1.17.0-i686-pc-windows-gnu.msi)
16+
- [Cargo (32-bit)](https://static.rust-lang.org/cargo-dist/cargo-nightly-i686-pc-windows-gnu.tar.gz)
17+
18+
- [Rust (64-bit)](https://static.rust-lang.org/dist/rust-1.17.0-x86_64-pc-windows-gnu.msi)
19+
- [Cargo (64-bit)](https://static.rust-lang.org/cargo-dist/cargo-nightly-x86_64-pc-windows-gnu.tar.gz)
20+
21+
Alternatively, you can [build Cargo from source](https://github.com/rust-lang/cargo#compiling-from-source).

src/doc/book/src/01-02-first-steps.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
## First Steps with Cargo
2+
3+
To start a new project with Cargo, use `cargo new`:
4+
5+
```shell
6+
$ cargo new hello_world --bin
7+
```
8+
9+
We’re passing `--bin` because we’re making a binary program: if we
10+
were making a library, we’d leave it off.
11+
12+
Let’s check out what Cargo has generated for us:
13+
14+
```shell
15+
$ cd hello_world
16+
$ tree .
17+
.
18+
├── Cargo.toml
19+
└── src
20+
└── main.rs
21+
22+
1 directory, 2 files
23+
```
24+
25+
This is all we need to get started. First, let’s check out `Cargo.toml`:
26+
27+
```toml
28+
[package]
29+
name = "hello_world"
30+
version = "0.1.0"
31+
authors = ["Your Name <[email protected]>"]
32+
```
33+
34+
This is called a **manifest**, and it contains all of the metadata that Cargo
35+
needs to compile your project.
36+
37+
Here’s what’s in `src/main.rs`:
38+
39+
```
40+
fn main() {
41+
println!("Hello, world!");
42+
}
43+
```
44+
45+
Cargo generated a “hello world” for us. Let’s compile it:
46+
47+
```shell
48+
$ cargo build
49+
Compiling hello_world v0.1.0 (file:///path/to/project/hello_world)
50+
```
51+
52+
And then run it:
53+
54+
```shell
55+
$ ./target/debug/hello_world
56+
Hello, world!
57+
```
58+
59+
We can also use `cargo run` to compile and then run it, all in one step:
60+
61+
```shell
62+
$ cargo run
63+
Fresh hello_world v0.1.0 (file:///path/to/project/hello_world)
64+
Running `target/hello_world`
65+
Hello, world!
66+
```
67+
68+
## Going further
69+
70+
For more details on using Cargo, check out the [Cargo Guide](guide.html)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
## Why Cargo Exists
2+
3+
Cargo is a tool that allows Rust projects to declare their various
4+
dependencies and ensure that you’ll always get a repeatable build.
5+
6+
To accomplish this goal, Cargo does four things:
7+
8+
* Introduces two metadata files with various bits of project information.
9+
* Fetches and builds your project’s dependencies.
10+
* Invokes `rustc` or another build tool with the correct parameters to build
11+
your project.
12+
* Introduces conventions to make working with Rust projects easier.
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
## Creating a New Project
2+
3+
To start a new project with Cargo, use `cargo new`:
4+
5+
```shell
6+
$ cargo new hello_world --bin
7+
```
8+
9+
We’re passing `--bin` because we’re making a binary program: if we
10+
were making a library, we’d leave it off. This also initializes a new `git`
11+
repository by default. If you don't want it to do that, pass `--vcs none`.
12+
13+
Let’s check out what Cargo has generated for us:
14+
15+
```shell
16+
$ cd hello_world
17+
$ tree .
18+
.
19+
├── Cargo.toml
20+
└── src
21+
└── main.rs
22+
23+
1 directory, 2 files
24+
```
25+
26+
If we had just used `cargo new hello_world` without the `--bin` flag, then
27+
we would have a `lib.rs` instead of a `main.rs`. For now, however, this is all
28+
we need to get started. First, let’s check out `Cargo.toml`:
29+
30+
```toml
31+
[package]
32+
name = "hello_world"
33+
version = "0.1.0"
34+
authors = ["Your Name <[email protected]>"]
35+
```
36+
37+
This is called a **manifest**, and it contains all of the metadata that Cargo
38+
needs to compile your project.
39+
40+
Here’s what’s in `src/main.rs`:
41+
42+
```
43+
fn main() {
44+
println!("Hello, world!");
45+
}
46+
```
47+
48+
Cargo generated a “hello world” for us. Let’s compile it:
49+
50+
```shell
51+
$ cargo build
52+
Compiling hello_world v0.1.0 (file:///path/to/project/hello_world)
53+
```
54+
55+
And then run it:
56+
57+
```shell
58+
$ ./target/debug/hello_world
59+
Hello, world!
60+
```
61+
62+
We can also use `cargo run` to compile and then run it, all in one step (You
63+
won't see the `Compiling` line if you have not made any changes since you last
64+
compiled):
65+
66+
```shell
67+
$ cargo run
68+
Compiling hello_world v0.1.0 (file:///path/to/project/hello_world)
69+
Running `target/debug/hello_world`
70+
Hello, world!
71+
```
72+
73+
You’ll now notice a new file, `Cargo.lock`. It contains information about our
74+
dependencies. Since we don’t have any yet, it’s not very interesting.
75+
76+
Once you’re ready for release, you can use `cargo build --release` to compile
77+
your files with optimizations turned on:
78+
79+
```shell
80+
$ cargo build --release
81+
Compiling hello_world v0.1.0 (file:///path/to/project/hello_world)
82+
```
83+
84+
`cargo build --release` puts the resulting binary in `target/release` instead of
85+
`target/debug`.
86+
87+
Compiling in debug mode is the default for development-- compilation time is
88+
shorter since the compiler doesn't do optimizations, but the code will run
89+
slower. Release mode takes longer to compile, but the code will run faster.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
## Working on an Existing Cargo Project
2+
3+
If you download an existing project that uses Cargo, it’s really easy
4+
to get going.
5+
6+
First, get the project from somewhere. In this example, we’ll use `rand`
7+
cloned from its repository on GitHub:
8+
9+
```shell
10+
$ git clone https://github.com/rust-lang-nursery/rand.git
11+
$ cd rand
12+
```
13+
14+
To build, use `cargo build`:
15+
16+
```shell
17+
$ cargo build
18+
Compiling rand v0.1.0 (file:///path/to/project/rand)
19+
```
20+
21+
This will fetch all of the dependencies and then build them, along with the
22+
project.
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
## Dependencies
2+
3+
[crates.io] is the Rust community's central package registry that serves as a
4+
location to discover and download packages. `cargo` is configured to use it by
5+
default to find requested packages.
6+
7+
To depend on a library hosted on [crates.io], add it to your `Cargo.toml`.
8+
9+
[crates.io]: https://crates.io/
10+
11+
### Adding a dependency
12+
13+
If your `Cargo.toml` doesn't already have a `[dependencies]` section, add that,
14+
then list the crate name and version that you would like to use. This example
15+
adds a dependency of the `time` crate:
16+
17+
```toml
18+
[dependencies]
19+
time = "0.1.12"
20+
```
21+
22+
The version string is a [semver] version requirement. The [specifying
23+
dependencies](03-01-specifying-dependencies.html) docs have more information about
24+
the options you have here.
25+
26+
[semver]: https://github.com/steveklabnik/semver#requirements
27+
28+
If we also wanted to add a dependency on the `regex` crate, we would not need
29+
to add `[dependencies]` for each crate listed. Here's what your whole
30+
`Cargo.toml` file would look like with dependencies on the `time` and `regex`
31+
crates:
32+
33+
```toml
34+
[package]
35+
name = "hello_world"
36+
version = "0.1.0"
37+
authors = ["Your Name <[email protected]>"]
38+
39+
[dependencies]
40+
time = "0.1.12"
41+
regex = "0.1.41"
42+
```
43+
44+
Re-run `cargo build`, and Cargo will fetch the new dependencies and all of
45+
their dependencies, compile them all, and update the `Cargo.lock`:
46+
47+
```shell
48+
$ cargo build
49+
Updating registry `https://github.com/rust-lang/crates.io-index`
50+
Downloading memchr v0.1.5
51+
Downloading libc v0.1.10
52+
Downloading regex-syntax v0.2.1
53+
Downloading memchr v0.1.5
54+
Downloading aho-corasick v0.3.0
55+
Downloading regex v0.1.41
56+
Compiling memchr v0.1.5
57+
Compiling libc v0.1.10
58+
Compiling regex-syntax v0.2.1
59+
Compiling memchr v0.1.5
60+
Compiling aho-corasick v0.3.0
61+
Compiling regex v0.1.41
62+
Compiling hello_world v0.1.0 (file:///path/to/project/hello_world)
63+
```
64+
65+
Our `Cargo.lock` contains the exact information about which revision of all of
66+
these dependencies we used.
67+
68+
Now, if `regex` gets updated, we will still build with the same revision until
69+
we choose to `cargo update`.
70+
71+
You can now use the `regex` library using `extern crate` in `main.rs`.
72+
73+
```
74+
extern crate regex;
75+
76+
use regex::Regex;
77+
78+
fn main() {
79+
let re = Regex::new(r"^\d{4}-\d{2}-\d{2}$").unwrap();
80+
println!("Did our date match? {}", re.is_match("2014-01-01"));
81+
}
82+
```
83+
84+
Running it will show:
85+
86+
```shell
87+
$ cargo run
88+
Running `target/hello_world`
89+
Did our date match? true
90+
```

0 commit comments

Comments
 (0)