Skip to content

Commit 9991af3

Browse files
committed
Auto merge of #8855 - salewski:ads/issue-8854-doc-essential-terms, r=alexcrichton
doc (book): add "Getting Started" subsection: "Essential Terminology" # Reworked PR (2020-11-21) Fixes #8854 In The Cargo Book, beef-up the glossary, and provide hyperlinks to the definitions of key terms through both the "Getting Started" and "Cargo Guide" chapters. In addition to the links, some transitional text (two sentences) has been added to the beginning of "First Steps with Cargo", and somewhat more to "Why Cargo Exists" in the guide. The intent is cater to readers jumping into this book cold, or perhaps coming from the "Hello, Cargo!" section at the beginning of The Book. The changes assume a knowledgeable but skeptical user, so take pains to justify the reader's attention. A developer that knows very little about Rust and nothing about Cargo and can follow the text and map Cargo-specific concepts to analogues in other language ecosystems. Within the glossary many terms are touched-up, as described in the commit message for the first patch in the series. Intra-glossary links are added between all terms. In the "Getting Started" and "Cargo Guide" chapters, terms are linked to their glossary entries throughout, for the first mention of a term within a given *section*. The first time a term is seen in the *chapter*, the markdown syntax for italics is used. In "First Steps with Cargo", both italics and bold markdown are used, in keeping with the existing styling. # Orig PR (2020-11-12) -- obsolete ~~Insert short terminology cheatsheet page to explain these terms:~~ * ~~package~~ * ~~crate~~ - ~~library crate~~ - ~~binary crate~~ * ~~module~~ * ~~workspace~~ ~~Important in these definitions is the fact that a single package is defined by a single 'Cargo.toml' file, and that a non-workspace Cargo.toml package contains one or more crates.~~ ~~The last two terms in the list are just glossed over, as it is more important that the user know that the terms exist, and that there are both lower-level (module) and higher-level (workspace) concepts than the organizational units (a package and its crates) that feature most prominently in basic cargo builds.~~ ~~With the addition of this new "Essential Terminology" page, the structure at the beginning of The Cargo Book is:~~ Introduction 1. Getting Started 1.1. Essential Terminology <= new 1.2. Installation 1.3. First Steps with Cargo 2. Cargo Guide ... ~~The terms in the above list are all well defined elsewhere in the Rust and Cargo documentation, but a new user encountering them for the first time should not need to hunt them down.~~ ~~Since the opening sections of the Cargo book are among the first documents a new user is likely to encounter, we are providing a little cheatsheet of the essentials at this stage. Defining the above terms gives the reader a good foothold to map concepts they may be familiar with from other languages onto their Rust counterparts (roughly speaking).~~ ~~Closes #8854~~
2 parents bfca1cd + ab78221 commit 9991af3

14 files changed

+259
-85
lines changed

src/doc/src/appendix/glossary.md

Lines changed: 117 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,50 @@
33
### Artifact
44

55
An *artifact* is the file or set of files created as a result of the
6-
compilation process. This includes linkable libraries and executable binaries.
6+
compilation process. This includes linkable libraries, executable binaries,
7+
and generated documentation.
8+
9+
### Cargo
10+
11+
*Cargo* is the Rust [*package manager*](#package-manager), and the primary
12+
topic of this book.
13+
14+
### Cargo.lock
15+
16+
See [*lock file*](#lock-file).
17+
18+
### Cargo.toml
19+
20+
See [*manifest*](#manifest).
721

822
### Crate
923

10-
Every target in a package is a *crate*. Crates are either libraries or
11-
executable binaries. It may loosely refer to either the source code of the
12-
target, or the compiled artifact that the target produces. A crate may also
13-
refer to a compressed package fetched from a registry.
24+
A Rust *crate* is either a library or an executable program, referred to as
25+
either a *library crate* or a *binary crate*, respectively.
26+
27+
Every [target](#target) defined for a Cargo [package](#package) is a *crate*.
28+
29+
Loosely, the term *crate* may refer to either the source code of the target or
30+
to the compiled artifact that the target produces. It may also refer to a
31+
compressed package fetched from a [registry](#registry).
32+
33+
The source code for a given crate may be subdivided into [*modules*](#module).
1434

1535
### Edition
1636

1737
A *Rust edition* is a developmental landmark of the Rust language. The
1838
[edition of a package][edition-field] is specified in the `Cargo.toml`
19-
manifest, and individual targets can specify which edition they use. See the
20-
[Edition Guide] for more information.
39+
[manifest](#manifest), and individual targets can specify which edition they
40+
use. See the [Edition Guide] for more information.
2141

2242
### Feature
2343

2444
The meaning of *feature* depends on the context:
2545

2646
- A [*feature*][feature] is a named flag which allows for conditional
2747
compilation. A feature can refer to an optional dependency, or an arbitrary
28-
name defined in a `Cargo.toml` manifest that can be checked within source
29-
code.
48+
name defined in a `Cargo.toml` [manifest](#manifest) that can be checked
49+
within source code.
3050

3151
- Cargo has [*unstable feature flags*][cargo-unstable] which can be used to
3252
enable experimental behavior of Cargo itself.
@@ -40,55 +60,107 @@ The meaning of *feature* depends on the context:
4060

4161
### Index
4262

43-
The index is the searchable list of crates in a registry.
63+
The *index* is the searchable list of [*crates*](#crate) in a
64+
[*registry*](#registry).
4465

4566
### Lock file
4667

4768
The `Cargo.lock` *lock file* is a file that captures the exact version of
48-
every dependency used in a workspace or package. It is automatically generated
49-
by Cargo. See [Cargo.toml vs Cargo.lock].
69+
every dependency used in a [*workspace*](#workspace) or
70+
[*package*](#package). It is automatically generated by Cargo. See
71+
[Cargo.toml vs Cargo.lock].
5072

5173
### Manifest
5274

53-
A [*manifest*][manifest] is a description of a package or a workspace in a
54-
file named `Cargo.toml`.
75+
A [*manifest*][manifest] is a description of a [package](#package) or a
76+
[workspace](#workspace) in a file named `Cargo.toml`.
5577

5678
A [*virtual manifest*][virtual] is a `Cargo.toml` file that only describes a
5779
workspace, and does not include a package.
5880

5981
### Member
6082

61-
A *member* is a package that belongs to a workspace.
83+
A *member* is a [*package*](#package) that belongs to a
84+
[*workspace*](#workspace).
85+
86+
### Module
87+
88+
Rust's module system is used to organize code into logical units called
89+
*modules*, which provide isolated namespaces within the code.
90+
91+
The source code for a given [crate](#crate) may be subdivided into one or more
92+
separate modules. This is usually done to organize the code into areas of
93+
related functionality or to control the visible scope (public/private) of
94+
symbols within the source (structs, functions, and so on).
95+
96+
A [`Cargo.toml`](#manifest) file is primarily concerned with the
97+
[package](#package) it defines, its crates, and the packages of the crates on
98+
which they depend. Nevertheless, you will see the term "module" often when
99+
working with Rust, so you should understand its relationship to a given crate.
62100

63101
### Package
64102

65-
A *package* is a collection of source files and a `Cargo.toml` manifest which
66-
describes the package. A package has a name and version which is used for
67-
specifying dependencies between packages. A package contains multiple targets,
68-
which are either libraries or executable binaries.
103+
A *package* is a collection of source files and a `Cargo.toml`
104+
[*manifest*](#manifest) file which describes the package. A package has a name
105+
and version which is used for specifying dependencies between packages.
106+
107+
A package contains multiple [*targets*](#target), each of which is a
108+
[*crate*](#crate). The `Cargo.toml` file describes the type of the crates
109+
(binary or library) within the package, along with some metadata about each
110+
one -- how each is to be built, what their direct dependencies are, etc., as
111+
described throughout this book.
69112

70113
The *package root* is the directory where the package's `Cargo.toml` manifest
71-
is located.
114+
is located. (Compare with [*workspace root*](#workspace).)
72115

73116
The [*package ID specification*][pkgid-spec], or *SPEC*, is a string used to
74117
uniquely reference a specific version of a package from a specific source.
75118

119+
Small to medium sized Rust projects will only need a single package, though it
120+
is common for them to have multiple crates.
121+
122+
Larger projects may involve multiple packages, in which case Cargo
123+
[*workspaces*](#workspace) can be used to manage common dependencies and other
124+
related metadata between the packages.
125+
126+
### Package manager
127+
128+
Broadly speaking, a *package manager* is a program (or collection of related
129+
programs) in a software ecosystem that automates the process of obtaining,
130+
installing, and upgrading artifacts. Within a programming language ecosystem,
131+
a package manager is a developer-focused tool whose primary functionality is
132+
to download library artifacts and their dependencies from some central
133+
repository; this capability is often combined with the ability to perform
134+
software builds (by invoking the language-specific compiler).
135+
136+
[*Cargo*](#cargo) is the package manager within the Rust ecosystem. Cargo
137+
downloads your Rust [package](#package)’s dependencies
138+
([*artifacts*](#artifact) known as [*crates*](#crate)), compiles your
139+
packages, makes distributable packages, and (optionally) uploads them to
140+
[crates.io][], the Rust community’s [*package registry*](#registry).
141+
142+
### Package registry
143+
144+
See [*registry*](#registry).
145+
76146
### Project
77147

78148
Another name for a [package](#package).
79149

80150
### Registry
81151

82-
A *registry* is a service that contains a collection of downloadable crates
83-
that can be installed or used as dependencies for a package. The default
84-
registry is [crates.io](https://crates.io). The registry has an *index* which
152+
A *registry* is a service that contains a collection of downloadable
153+
[*crates*](#crate) that can be installed or used as dependencies for a
154+
[*package*](#package). The default registry in the Rust ecosystem is
155+
[crates.io](https://crates.io). The registry has an [*index*](#index) which
85156
contains a list of all crates, and tells Cargo how to download the crates that
86157
are needed.
87158

88159
### Source
89160

90-
A *source* is a provider that contains crates that may be included as
91-
dependencies for a package. There are several kinds of sources:
161+
A *source* is a provider that contains [*crates*](#crate) that may be included
162+
as dependencies for a [*package*](#package). There are several kinds of
163+
sources:
92164

93165
- **Registry source** — See [registry](#registry).
94166
- **Local registry source** — A set of crates stored as compressed files on
@@ -110,16 +182,17 @@ See [package ID specification](#package).
110182

111183
The meaning of the term *target* depends on the context:
112184

113-
- **Cargo Target** — Cargo packages consist of *targets* which correspond to
114-
artifacts that will be produced. Packages can have library, binary, example,
115-
test, and benchmark targets. The [list of targets][targets] are configured
116-
in the `Cargo.toml` manifest, often inferred automatically by the [directory
185+
- **Cargo Target** — Cargo [*packages*](#package) consist of *targets* which
186+
correspond to [*artifacts*](#artifact) that will be produced. Packages can
187+
have library, binary, example, test, and benchmark targets. The
188+
[list of targets][targets] are configured in the `Cargo.toml`
189+
[*manifest*](#manifest), often inferred automatically by the [directory
117190
layout] of the source files.
118191
- **Target Directory** — Cargo places all built artifacts and intermediate
119192
files in the *target* directory. By default this is a directory named
120-
`target` at the workspace root, or the package root if not using a
121-
workspace. The directory may be changed with the `--target-dir` command-line
122-
option, the `CARGO_TARGET_DIR` [environment variable], or the
193+
`target` at the [*workspace*](#workspace) root, or the package root if not
194+
using a workspace. The directory may be changed with the `--target-dir`
195+
command-line option, the `CARGO_TARGET_DIR` [environment variable], or the
123196
`build.target-dir` [config option].
124197
- **Target Architecture** — The OS and machine architecture for the built
125198
artifacts are typically referred to as a *target*.
@@ -154,22 +227,24 @@ correctness of code. There are two types of test artifacts:
154227
individual units of code.
155228
* **Integration test target** — An [*integration test
156229
target*][integration-tests] is an executable binary compiled from a *test
157-
target* which is a distinct crate whose source is located in the `tests`
158-
directory or specified by the [`[[test]]` table][targets] in the
159-
`Cargo.toml` manifest. It is intended to only test the public API of a
160-
library, or execute a binary to verify its operation.
230+
target* which is a distinct [*crate*](#crate) whose source is located in the
231+
`tests` directory or specified by the [`[[test]]` table][targets] in the
232+
`Cargo.toml` [*manifest*](#manifest). It is intended to only test the public
233+
API of a library, or execute a binary to verify its operation.
161234

162235
### Workspace
163236

164-
A [*workspace*][workspace] is a collection of one or more packages that share
165-
common dependency resolution (with a shared `Cargo.lock`), output directory,
166-
and various settings such as profiles.
237+
A [*workspace*][workspace] is a collection of one or more
238+
[*packages*](#package) that share common dependency resolution (with a shared
239+
`Cargo.lock` [*lock file*](#lock-file)), output directory, and various
240+
settings such as profiles.
167241

168242
A [*virtual workspace*][virtual] is a workspace where the root `Cargo.toml`
169-
manifest does not define a package, and only lists the workspace members.
243+
[*manifest*](#manifest) does not define a package, and only lists the
244+
workspace [*members*](#member).
170245

171246
The *workspace root* is the directory where the workspace's `Cargo.toml`
172-
manifest is located.
247+
manifest is located. (Compare with [*package root*](#package).)
173248

174249

175250
[Cargo.toml vs Cargo.lock]: ../guide/cargo-toml-vs-cargo-lock.md
@@ -178,6 +253,7 @@ manifest is located.
178253
[Source Replacement]: ../reference/source-replacement.md
179254
[cargo-unstable]: ../reference/unstable.md
180255
[config option]: ../reference/config.md
256+
[crates.io]: https://crates.io/
181257
[directory layout]: ../guide/project-layout.md
182258
[edition guide]: ../../edition-guide/index.html
183259
[edition-field]: ../reference/manifest.md#the-edition-field

src/doc/src/getting-started/first-steps.md

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
## First Steps with Cargo
22

3+
This section provides a quick sense for the `cargo` command line tool. We
4+
demonstrate its ability to generate a new [***package***][def-package] for us,
5+
its ability to compile the [***crate***][def-crate] within the package, and
6+
its ability to run the resulting program.
7+
38
To start a new package with Cargo, use `cargo new`:
49

510
```console
611
$ cargo new hello_world
712
```
813

9-
Cargo defaults to `--bin` to make a binary program. To make a library, we'd
10-
pass `--lib`.
14+
Cargo defaults to `--bin` to make a binary program. To make a library, we
15+
would pass `--lib`, instead.
1116

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

@@ -34,8 +39,8 @@ edition = "2018"
3439
[dependencies]
3540
```
3641

37-
This is called a **manifest**, and it contains all of the metadata that Cargo
38-
needs to compile your package.
42+
This is called a [***manifest***][def-manifest], and it contains all of the
43+
metadata that Cargo needs to compile your package.
3944

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

@@ -45,7 +50,8 @@ fn main() {
4550
}
4651
```
4752

48-
Cargo generated a “hello world” for us. Let’s compile it:
53+
Cargo generated a “hello world” program for us, otherwise known as a
54+
[***binary crate***][def-crate]. Let’s compile it:
4955

5056
```console
5157
$ cargo build
@@ -71,3 +77,7 @@ Hello, world!
7177
### Going further
7278

7379
For more details on using Cargo, check out the [Cargo Guide](../guide/index.md)
80+
81+
[def-crate]: ../appendix/glossary.md#crate '"crate" (glossary entry)'
82+
[def-manifest]: ../appendix/glossary.md#manifest '"manifest" (glossary entry)'
83+
[def-package]: ../appendix/glossary.md#package '"package" (glossary entry)'

src/doc/src/getting-started/index.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
## Getting Started
22

3-
To get started with Cargo, install Cargo (and Rust) and set up your first crate.
3+
To get started with Cargo, install Cargo (and Rust) and set up your first
4+
[*crate*][def-crate].
45

56
* [Installation](installation.md)
67
* [First steps with Cargo](first-steps.md)
8+
9+
[def-crate]: ../appendix/glossary.md#crate '"crate" (glossary entry)'

src/doc/src/guide/build-cache.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
## Build cache
22

33
Cargo stores the output of a build into the "target" directory. By default,
4-
this is the directory named `target` in the root of your workspace. To change
5-
the location, you can set the `CARGO_TARGET_DIR` [environment variable], the
6-
[`build.target-dir`] config value, or the `--target-dir` command-line flag.
4+
this is the directory named `target` in the root of your
5+
[*workspace*][def-worksapce]. To change the location, you can set the
6+
`CARGO_TARGET_DIR` [environment variable], the [`build.target-dir`] config
7+
value, or the `--target-dir` command-line flag.
78

89
The directory layout depends on whether or not you are using the `--target`
910
flag to build for a specific platform. If `--target` is not specified, Cargo
@@ -90,6 +91,7 @@ configuration][config]. Refer to sccache documentation for more details.
9091
[`cargo publish`]: ../commands/cargo-publish.md
9192
[build scripts]: ../reference/build-scripts.md
9293
[config]: ../reference/config.md
94+
[def-workspace]: ../appendix/glossary.md#workspace '"workspace" (glossary entry)'
9395
[environment variable]: ../reference/environment-variables.md
9496
[incremental output]: ../reference/profiles.md#incremental
9597
[sccache]: https://github.com/mozilla/sccache

src/doc/src/guide/cargo-home.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
## Cargo Home
22

33
The "Cargo home" functions as a download and source cache.
4-
When building a crate, Cargo stores downloaded build dependencies in the Cargo home.
4+
When building a [crate][def-crate], Cargo stores downloaded build dependencies in the Cargo home.
55
You can alter the location of the Cargo home by setting the `CARGO_HOME` [environmental variable][env].
66
The [home](https://crates.io/crates/home) crate provides an API for getting this location if you need this information inside your Rust crate.
77
By default, the Cargo home is located in `$HOME/.cargo/`.
@@ -16,10 +16,10 @@ The Cargo home consists of following components:
1616
Cargo's global configuration file, see the [config entry in the reference][config].
1717

1818
* `credentials.toml`
19-
Private login credentials from [`cargo login`] in order to log in to a registry.
19+
Private login credentials from [`cargo login`] in order to log in to a [registry][def-registry].
2020

2121
* `.crates.toml`
22-
This hidden file contains package information of crates installed via [`cargo install`]. Do NOT edit by hand!
22+
This hidden file contains [package][def-package] information of crates installed via [`cargo install`]. Do NOT edit by hand!
2323

2424
## Directories:
2525

@@ -83,4 +83,7 @@ Alternatively, the [cargo-cache](https://crates.io/crates/cargo-cache) crate pro
8383
[`cargo login`]: ../commands/cargo-login.md
8484
[`cargo vendor`]: ../commands/cargo-vendor.md
8585
[config]: ../reference/config.md
86+
[def-crate]: ../appendix/glossary.md#crate '"crate" (glossary entry)'
87+
[def-package]: ../appendix/glossary.md#package '"package" (glossary entry)'
88+
[def-registry]: ../appendix/glossary.md#registry '"registry" (glossary entry)'
8689
[env]: ../reference/environment-variables.md

src/doc/src/guide/cargo-toml-vs-cargo-lock.md

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,20 @@ about them, here’s a summary:
88
* `Cargo.lock` contains exact information about your dependencies. It is
99
maintained by Cargo and should not be manually edited.
1010

11-
If you’re building a non-end product, such as a rust library that other rust packages will depend on, put
12-
`Cargo.lock` in your `.gitignore`. If you’re building an end product, which are executable
13-
like command-line tool or an application, or a system library with crate-type of `staticlib` or `cdylib`,
14-
check `Cargo.lock` into `git`. If you're curious about why that is, see
11+
If you’re building a non-end product, such as a rust library that other rust
12+
[packages][def-package] will depend on, put `Cargo.lock` in your
13+
`.gitignore`. If you’re building an end product, which are executable like
14+
command-line tool or an application, or a system library with crate-type of
15+
`staticlib` or `cdylib`, check `Cargo.lock` into `git`. If you're curious
16+
about why that is, see
1517
["Why do binaries have `Cargo.lock` in version control, but not libraries?" in the
1618
FAQ](../faq.md#why-do-binaries-have-cargolock-in-version-control-but-not-libraries).
1719

1820
Let’s dig in a little bit more.
1921

20-
`Cargo.toml` is a **manifest** file in which we can specify a bunch of
21-
different metadata about our package. For example, we can say that we depend
22-
on another package:
22+
`Cargo.toml` is a [**manifest**][def-manifest] file in which we can specify a
23+
bunch of different metadata about our package. For example, we can say that we
24+
depend on another package:
2325

2426
```toml
2527
[package]
@@ -101,3 +103,6 @@ This will write out a new `Cargo.lock` with the new version information. Note
101103
that the argument to `cargo update` is actually a
102104
[Package ID Specification](../reference/pkgid-spec.md) and `rand` is just a short
103105
specification.
106+
107+
[def-manifest]: ../appendix/glossary.md#manifest '"manifest" (glossary entry)'
108+
[def-package]: ../appendix/glossary.md#package '"package" (glossary entry)'

0 commit comments

Comments
 (0)