Skip to content

Commit 6e968e4

Browse files
committed
Add docs converted to mdbook
1 parent 52673c8 commit 6e968e4

29 files changed

+3360
-0
lines changed
+21
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

+70
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)
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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 your project.
11+
* Introduces conventions to make working with Rust projects easier.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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 notice several new files and directories have been created:
74+
```shell
75+
$ tree .
76+
.
77+
├── Cargo.lock
78+
├── Cargo.toml
79+
├── src
80+
│   └── main.rs
81+
└── target
82+
└── debug
83+
├── build
84+
├── deps
85+
│   └── hello_world-2386c2fd0156916f
86+
├── examples
87+
├── hello_world
88+
├── hello_world.d
89+
├── incremental
90+
└── native
91+
92+
8 directories, 6 files
93+
```
94+
95+
The `Cargo.lock` file contains information about our dependencies. Since we
96+
don’t have any yet, it’s not very interesting. The `target` directory contains
97+
all the build products, and, as can be seen, Cargo produces debug builds by
98+
default. You can use `cargo build --release` to compile your files with
99+
optimizations turned on:
100+
101+
```shell
102+
$ cargo build --release
103+
Compiling hello_world v0.1.0 (file:///path/to/project/hello_world)
104+
```
105+
106+
`cargo build --release` puts the resulting binary in `target/release`
107+
instead of `target/debug`.
108+
109+
Compiling in debug mode is the default for development -- compilation time is
110+
shorter since the compiler doesn't do optimizations, but the code will run
111+
slower. Release mode takes longer to compile, but the code will run faster.
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.
+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
## Adding dependencies from crates.io
2+
3+
[crates.io] is the Rust community's central repository that serves
4+
as a location to discover and download packages. `cargo` is configured to use
5+
it by 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)