-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
52673c8
commit 6e968e4
Showing
29 changed files
with
3,360 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 <you@example.com>"] | ||
``` | ||
|
||
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
## 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
## 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 <you@example.com>"] | ||
``` | ||
|
||
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 notice several new files and directories have been created: | ||
```shell | ||
$ tree . | ||
. | ||
├── Cargo.lock | ||
├── Cargo.toml | ||
├── src | ||
│ └── main.rs | ||
└── target | ||
└── debug | ||
├── build | ||
├── deps | ||
│ └── hello_world-2386c2fd0156916f | ||
├── examples | ||
├── hello_world | ||
├── hello_world.d | ||
├── incremental | ||
└── native | ||
|
||
8 directories, 6 files | ||
``` | ||
|
||
The `Cargo.lock` file contains information about our dependencies. Since we | ||
don’t have any yet, it’s not very interesting. The `target` directory contains | ||
all the build products, and, as can be seen, Cargo produces debug builds by | ||
default. 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
## Adding dependencies from crates.io | ||
|
||
[crates.io] is the Rust community's central repository 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 <you@example.com>"] | ||
|
||
[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 | ||
``` |
Oops, something went wrong.