Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
4d590f9
[src/doc/book] Import from cargo-book repo
behnam Aug 30, 2017
a442f85
[src/doc/book] Add .gitignore
behnam Aug 30, 2017
e62845c
[src/doc/book] Update 03-03-config.md from config.md
behnam Aug 31, 2017
b8820de
[src/doc/book] Update 03-05-build-scripts.md from build-script.md
behnam Aug 31, 2017
0776699
[src/doc/book] Update 03-06-crates-io.md from crates-io.md
behnam Aug 31, 2017
93baf1e
[src/doc/book] Update 03-04-environment-variables.md from environment…
behnam Aug 31, 2017
ae7889a
[src/doc/book] Update 03-09-external-tools.md from external-tools.md
behnam Aug 31, 2017
72590a3
[src/doc/book] Check faq.md
behnam Aug 31, 2017
582f46c
[src/doc/book] Update guide.md and 02-*.md from guide.md
behnam Aug 31, 2017
53d5495
[src/doc/book] Apply 01-*.md changes into index.md
behnam Aug 31, 2017
917c363
[src/doc/book] Update 03-02-manifest.md from manifest.md
behnam Aug 31, 2017
51be221
[src/doc/book] Check 03-07-pkgid-spec.md
behnam Aug 31, 2017
055147f
[src/doc/book] Update 03-10-policies.md from policies.md
behnam Aug 31, 2017
d431fa2
[src/doc/book] Check 03-08-source-replacement.md
behnam Aug 31, 2017
d7c5a6b
[src/doc/book] Update 03-01-specifying-dependencies.md from specifyin…
behnam Aug 31, 2017
0fdd599
[src/doc/book] Update SUMMARY.md
behnam Aug 31, 2017
ec25b8b
[src/doc] Add README.md
behnam Aug 31, 2017
e6841b9
[src/doc/book] Drop unused images
behnam Aug 31, 2017
afe693d
[src/doc/book] Add all sub-chapters to cargo-in-depth.md
behnam Aug 31, 2017
01aa9e3
[src/doc/book] Move a paragraph to cargo-in-depth.md
behnam Aug 31, 2017
3fac7b2
[src/doc/book] Fix header casings
behnam Aug 31, 2017
bd5ecd4
[src/doc/book] Use package registry for crates.io
behnam Aug 31, 2017
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
13 changes: 13 additions & 0 deletions src/doc/MIGRATION_MAP
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
build-script.md book/src/03-05-build-scripts.md
config.md book/src/03-03-config.md
crates-io.md book/src/03-06-crates-io.md
environment-variables.md book/src/03-04-environment-variables.md
external-tools.md book/src/03-09-external-tools.md
faq.md book/src/faq.md
guide.md book/src/guide.md book/src/02-*.md
index.md book/src/SUMMARY.md book/src/01-*.md
manifest.md book/src/03-02-manifest.md
pkgid-spec.md book/src/03-07-pkgid-spec.md
policies.md book/src/03-10-policies.md
source-replacement.md book/src/03-08-source-replacement.md
specifying-dependencies.md book/src/03-01-specifying-dependencies.md
6 changes: 6 additions & 0 deletions src/doc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Cargo Documentation

NOTE: Cargo documentation is under migration to mdBook-based structure. All the
`*.md` files here shall be kept in sync with the `*.md` files under `book/src/`.
See `MIGRATION_MAP` file here and <https://github.com/rust-lang/cargo/pull/4453>
for details.
1 change: 1 addition & 0 deletions src/doc/book/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/book
47 changes: 47 additions & 0 deletions src/doc/book/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# The Cargo Book


### Requirements

Building the book requires [mdBook]. To get it:

[mdBook]: https://github.com/azerupi/mdBook

```bash
$ cargo install mdbook
```

### Building

To build the book:

```bash
$ mdbook build
```

The output will be in the `book` subdirectory. To check it out, open it in
your web browser.

_Firefox:_
```bash
$ firefox book/index.html # Linux
$ open -a "Firefox" book/index.html # OS X
$ Start-Process "firefox.exe" .\book\index.html # Windows (PowerShell)
$ start firefox.exe .\book\index.html # Windows (Cmd)
```

_Chrome:_
```bash
$ google-chrome book/index.html # Linux
$ open -a "Google Chrome" book/index.html # OS X
$ Start-Process "chrome.exe" .\book\index.html # Windows (PowerShell)
$ start chrome.exe .\book\index.html # Windows (Cmd)
```


## Contributing

Given that the book is still in a draft state, we'd love your help! Please feel free to open
issues about anything, and send in PRs for things you'd like to fix or change. If your change is
large, please open an issue first, so we can make sure that it's something we'd accept before you
go through the work of getting a PR together.
21 changes: 21 additions & 0 deletions src/doc/book/src/01-01-installation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
## Installation

The easiest way to get Cargo is to get the current stable release of Rust by
using the `rustup` script:

```shell
$ curl -sSf https://static.rust-lang.org/rustup.sh | sh
```

This will get you the current stable release of Rust for your platform along
with the latest Cargo.

If you are on Windows, you can directly download the latest stable Rust and nightly Cargo:

- [Rust (32-bit)](https://static.rust-lang.org/dist/rust-1.17.0-i686-pc-windows-gnu.msi)
- [Cargo (32-bit)](https://static.rust-lang.org/cargo-dist/cargo-nightly-i686-pc-windows-gnu.tar.gz)

- [Rust (64-bit)](https://static.rust-lang.org/dist/rust-1.17.0-x86_64-pc-windows-gnu.msi)
- [Cargo (64-bit)](https://static.rust-lang.org/cargo-dist/cargo-nightly-x86_64-pc-windows-gnu.tar.gz)

Alternatively, you can [build Cargo from source](https://github.com/rust-lang/cargo#compiling-from-source).
70 changes: 70 additions & 0 deletions src/doc/book/src/01-02-first-steps.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
## First Steps with Cargo

To start a new project with Cargo, use `cargo new`:

```shell
$ cargo new hello_world --bin
```

We’re passing `--bin` because we’re making a binary program: if we
were making a library, we’d leave it off.

Let’s check out what Cargo has generated for us:

```shell
$ cd hello_world
$ tree .
.
├── Cargo.toml
└── src
└── main.rs

1 directory, 2 files
```

This is all we need to get started. First, let’s check out `Cargo.toml`:

```toml
[package]
name = "hello_world"
version = "0.1.0"
authors = ["Your Name <[email protected]>"]
```

This is called a **manifest**, and it contains all of the metadata that Cargo
needs to compile your project.

Here’s what’s in `src/main.rs`:

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

Cargo generated a “hello world” for us. Let’s compile it:

```shell
$ cargo build
Compiling hello_world v0.1.0 (file:///path/to/project/hello_world)
```

And then run it:

```shell
$ ./target/debug/hello_world
Hello, world!
```

We can also use `cargo run` to compile and then run it, all in one step:

```shell
$ cargo run
Fresh hello_world v0.1.0 (file:///path/to/project/hello_world)
Running `target/hello_world`
Hello, world!
```

## Going further

For more details on using Cargo, check out the [Cargo Guide](guide.html)
12 changes: 12 additions & 0 deletions src/doc/book/src/02-01-why-cargo-exists.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
## Why Cargo Exists

Cargo is a tool that allows Rust projects to declare their various
dependencies and ensure that you’ll always get a repeatable build.

To accomplish this goal, Cargo does four things:

* Introduces two metadata files with various bits of project information.
* Fetches and builds your project’s dependencies.
* Invokes `rustc` or another build tool with the correct parameters to build
your project.
* Introduces conventions to make working with Rust projects easier.
89 changes: 89 additions & 0 deletions src/doc/book/src/02-02-creating-a-new-project.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
## Creating a New Project

To start a new project with Cargo, use `cargo new`:

```shell
$ cargo new hello_world --bin
```

We’re passing `--bin` because we’re making a binary program: if we
were making a library, we’d leave it off. This also initializes a new `git`
repository by default. If you don't want it to do that, pass `--vcs none`.

Let’s check out what Cargo has generated for us:

```shell
$ cd hello_world
$ tree .
.
├── Cargo.toml
└── src
└── main.rs

1 directory, 2 files
```

If we had just used `cargo new hello_world` without the `--bin` flag, then
we would have a `lib.rs` instead of a `main.rs`. For now, however, this is all
we need to get started. First, let’s check out `Cargo.toml`:

```toml
[package]
name = "hello_world"
version = "0.1.0"
authors = ["Your Name <[email protected]>"]
```

This is called a **manifest**, and it contains all of the metadata that Cargo
needs to compile your project.

Here’s what’s in `src/main.rs`:

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

Cargo generated a “hello world” for us. Let’s compile it:

```shell
$ cargo build
Compiling hello_world v0.1.0 (file:///path/to/project/hello_world)
```

And then run it:

```shell
$ ./target/debug/hello_world
Hello, world!
```

We can also use `cargo run` to compile and then run it, all in one step (You
won't see the `Compiling` line if you have not made any changes since you last
compiled):

```shell
$ cargo run
Compiling hello_world v0.1.0 (file:///path/to/project/hello_world)
Running `target/debug/hello_world`
Hello, world!
```

You’ll now notice a new file, `Cargo.lock`. It contains information about our
dependencies. Since we don’t have any yet, it’s not very interesting.

Once you’re ready for release, you can use `cargo build --release` to compile
your files with optimizations turned on:

```shell
$ cargo build --release
Compiling hello_world v0.1.0 (file:///path/to/project/hello_world)
```

`cargo build --release` puts the resulting binary in `target/release` instead of
`target/debug`.

Compiling in debug mode is the default for development-- compilation time is
shorter since the compiler doesn't do optimizations, but the code will run
slower. Release mode takes longer to compile, but the code will run faster.
22 changes: 22 additions & 0 deletions src/doc/book/src/02-03-working-on-an-existing-project.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
## Working on an Existing Cargo Project

If you download an existing project that uses Cargo, it’s really easy
to get going.

First, get the project from somewhere. In this example, we’ll use `rand`
cloned from its repository on GitHub:

```shell
$ git clone https://github.com/rust-lang-nursery/rand.git
$ cd rand
```

To build, use `cargo build`:

```shell
$ cargo build
Compiling rand v0.1.0 (file:///path/to/project/rand)
```

This will fetch all of the dependencies and then build them, along with the
project.
90 changes: 90 additions & 0 deletions src/doc/book/src/02-04-dependencies.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
## Dependencies

[crates.io] is the Rust community's central package registry that serves as a
location to discover and download packages. `cargo` is configured to use it by
default to find requested packages.

To depend on a library hosted on [crates.io], add it to your `Cargo.toml`.

[crates.io]: https://crates.io/

### Adding a dependency

If your `Cargo.toml` doesn't already have a `[dependencies]` section, add that,
then list the crate name and version that you would like to use. This example
adds a dependency of the `time` crate:

```toml
[dependencies]
time = "0.1.12"
```

The version string is a [semver] version requirement. The [specifying
dependencies](03-01-specifying-dependencies.html) docs have more information about
the options you have here.

[semver]: https://github.com/steveklabnik/semver#requirements

If we also wanted to add a dependency on the `regex` crate, we would not need
to add `[dependencies]` for each crate listed. Here's what your whole
`Cargo.toml` file would look like with dependencies on the `time` and `regex`
crates:

```toml
[package]
name = "hello_world"
version = "0.1.0"
authors = ["Your Name <[email protected]>"]

[dependencies]
time = "0.1.12"
regex = "0.1.41"
```

Re-run `cargo build`, and Cargo will fetch the new dependencies and all of
their dependencies, compile them all, and update the `Cargo.lock`:

```shell
$ cargo build
Updating registry `https://github.com/rust-lang/crates.io-index`
Downloading memchr v0.1.5
Downloading libc v0.1.10
Downloading regex-syntax v0.2.1
Downloading memchr v0.1.5
Downloading aho-corasick v0.3.0
Downloading regex v0.1.41
Compiling memchr v0.1.5
Compiling libc v0.1.10
Compiling regex-syntax v0.2.1
Compiling memchr v0.1.5
Compiling aho-corasick v0.3.0
Compiling regex v0.1.41
Compiling hello_world v0.1.0 (file:///path/to/project/hello_world)
```

Our `Cargo.lock` contains the exact information about which revision of all of
these dependencies we used.

Now, if `regex` gets updated, we will still build with the same revision until
we choose to `cargo update`.

You can now use the `regex` library using `extern crate` in `main.rs`.

```
extern crate regex;

use regex::Regex;

fn main() {
let re = Regex::new(r"^\d{4}-\d{2}-\d{2}$").unwrap();
println!("Did our date match? {}", re.is_match("2014-01-01"));
}
```

Running it will show:

```shell
$ cargo run
Running `target/hello_world`
Did our date match? true
```
Loading