]> git.proxmox.com Git - cargo.git/blame - src/doc/src/reference/manifest.md
Correct default cargo new edition
[cargo.git] / src / doc / src / reference / manifest.md
CommitLineData
4d590f95
BE
1## The Manifest Format
2
a133f25c
EH
3The `Cargo.toml` file for each package is called its *manifest*. It is written
4in the [TOML] format. Every manifest file consists of the following sections:
543e3817
EH
5
6* [`cargo-features`](unstable.md) — Unstable, nightly-only features.
7* [`[package]`](#the-package-section) — Defines a package.
8 * [`name`](#the-name-field) — The name of the package.
9 * [`version`](#the-version-field) — The version of the package.
10 * [`authors`](#the-authors-field) — The authors of the package.
11 * [`edition`](#the-edition-field) — The Rust edition.
12 * [`description`](#the-description-field) — A description of the package.
13 * [`documentation`](#the-documentation-field) — URL of the package documentation.
14 * [`readme`](#the-readme-field) — Path to the package's README file.
15 * [`homepage`](#the-homepage-field) — URL of the package homepage.
16 * [`repository`](#the-repository-field) — URL of the package source repository.
17 * [`license`](#the-license-and-license-file-fields) — The package license.
18 * [`license-file`](#the-license-and-license-file-fields) — Path to the text of the license.
19 * [`keywords`](#the-keywords-field) — Keywords for the package.
20 * [`categories`](#the-categories-field) — Categories of the package.
21 * [`workspace`](#the-workspace-field) — Path to the workspace for the package.
22 * [`build`](#the-build-field) — Path to the package build script.
23 * [`links`](#the-links-field) — Name of the native library the package links with.
24 * [`exclude`](#the-exclude-and-include-fields) — Files to exclude when publishing.
25 * [`include`](#the-exclude-and-include-fields) — Files to include when publishing.
26 * [`publish`](#the-publish-field) — Can be used to prevent publishing the package.
27 * [`metadata`](#the-metadata-table) — Extra settings for external tools.
28 * [`default-run`](#the-default-run-field) — The default binary to run by [`cargo run`].
29 * [`autobins`](cargo-targets.md#target-auto-discovery) — Disables binary auto discovery.
30 * [`autoexamples`](cargo-targets.md#target-auto-discovery) — Disables example auto discovery.
31 * [`autotests`](cargo-targets.md#target-auto-discovery) — Disables test auto discovery.
32 * [`autobenches`](cargo-targets.md#target-auto-discovery) — Disables bench auto discovery.
01bd9fbf 33 * [`resolver`](resolver.md#resolver-versions) — Sets the dependency resolver to use.
543e3817
EH
34* Target tables: (see [configuration](cargo-targets.md#configuring-a-target) for settings)
35 * [`[lib]`](cargo-targets.md#library) — Library target settings.
36 * [`[[bin]]`](cargo-targets.md#binaries) — Binary target settings.
37 * [`[[example]]`](cargo-targets.md#examples) — Example target settings.
38 * [`[[test]]`](cargo-targets.md#tests) — Test target settings.
39 * [`[[bench]]`](cargo-targets.md#benchmarks) — Benchmark target settings.
40* Dependency tables:
41 * [`[dependencies]`](specifying-dependencies.md) — Package library dependencies.
42 * [`[dev-dependencies]`](specifying-dependencies.md#development-dependencies) — Dependencies for examples, tests, and benchmarks.
43 * [`[build-dependencies]`](specifying-dependencies.md#build-dependencies) — Dependencies for build scripts.
44 * [`[target]`](specifying-dependencies.md#platform-specific-dependencies) — Platform-specific dependencies.
60779a00 45* [`[badges]`](#the-badges-section) — Badges to display on a registry.
543e3817 46* [`[features]`](features.md) — Conditional compilation features.
d1d08378
EH
47* [`[patch]`](overriding-dependencies.md#the-patch-section) — Override dependencies.
48* [`[replace]`](overriding-dependencies.md#the-replace-section) — Override dependencies (deprecated).
543e3817
EH
49* [`[profile]`](profiles.md) — Compiler settings and optimizations.
50* [`[workspace]`](workspaces.md) — The workspace definition.
79d10021 51
2a4aa420 52<a id="package-metadata"></a>
4d590f95
BE
53### The `[package]` section
54
55The first section in a `Cargo.toml` is `[package]`.
56
57```toml
58[package]
59name = "hello_world" # the name of the package
60version = "0.1.0" # the current version, obeying semver
59f35af8 61authors = ["Alice <a@example.com>", "Bob <b@example.com>"]
4d590f95
BE
62```
63
2a4aa420
EH
64The only fields required by Cargo are [`name`](#the-name-field) and
65[`version`](#the-version-field). If publishing to a registry, the registry may
66require additional fields. See the notes below and [the publishing
67chapter][publishing] for requirements for publishing to [crates.io].
68
e2074dfb
EH
69#### The `name` field
70
71The package name is an identifier used to refer to the package. It is used
72when listed as a dependency in another package, and as the default name of
73inferred lib and bin targets.
74
98752b00 75The name must use only [alphanumeric] characters or `-` or `_`, and cannot be empty.
7d7fe679 76Note that [`cargo new`] and [`cargo init`] impose some additional restrictions on
e2074dfb 77the package name, such as enforcing that it is a valid Rust identifier and not
da881b71 78a keyword. [crates.io] imposes even more restrictions, such as
e2074dfb
EH
79enforcing only ASCII characters, not a reserved name, not a special Windows
80name such as "nul", is not too long, etc.
81
b119b891 82[alphanumeric]: ../../std/primitive.char.html#method.is_alphanumeric
4d590f95
BE
83
84#### The `version` field
85
86Cargo bakes in the concept of [Semantic
0c3851c0 87Versioning](https://semver.org/), so make sure you follow some basic rules:
4d590f95
BE
88
89* Before you reach 1.0.0, anything goes, but if you make breaking changes,
90 increment the minor version. In Rust, breaking changes include adding fields to
91 structs or variants to enums.
92* After 1.0.0, only make breaking changes when you increment the major version.
93 Don’t break the build.
57b3e131 94* After 1.0.0, don’t add any new public API (no new `pub` anything) in patch-level
4d590f95
BE
95 versions. Always increment the minor version if you add any new `pub` structs,
96 traits, fields, types, functions, methods or anything else.
97* Use version numbers with three numeric parts such as 1.0.0 rather than 1.0.
98
fd6a84aa
EH
99See the [Resolver] chapter for more information on how Cargo uses versions to
100resolve dependencies, and for guidelines on setting your own version. See the
101[Semver compatibility] chapter for more details on exactly what constitutes a
102breaking change.
103
104[Resolver]: resolver.md
105[Semver compatibility]: semver.md
106
6e08c973 107<a id="the-authors-field-optional"></a>
2a4aa420 108#### The `authors` field
e2074dfb
EH
109
110The `authors` field lists people or organizations that are considered the
111"authors" of the package. The exact meaning is open to interpretation — it may
112list the original or primary authors, current maintainers, or owners of the
113package. These names will be listed on the crate's page on
da881b71 114[crates.io]. An optional email address may be included within angled
e2074dfb
EH
115brackets at the end of each author.
116
2a4aa420
EH
117> **Note**: [crates.io] requires at least one author to be listed.
118
6e08c973 119<a id="the-edition-field-optional"></a>
2a4aa420 120#### The `edition` field
3d029039 121
8ac5b520 122The `edition` key is an optional key that affects which edition your package
da5b0794
KH
123is compiled with. [`cargo new`] will generate a package with the `edition` key
124set to the latest edition. Setting the `edition` key in
8ac5b520
KH
125`[package]` will affect all targets/crates in the package, including test
126suites, benchmarks, binaries, examples, etc.
127
128If the `edition` key is not set to a specific [Rust Edition] in your
bc4cabd0 129`Cargo.toml`, Cargo will default to 2018.
3d029039
AC
130
131```toml
132[package]
133# ...
134edition = '2018'
135```
136
2a4aa420
EH
137#### The `description` field
138
139The description is a short blurb about the package. [crates.io] will display
140this with your package. This should be plain text (not Markdown).
141
142```toml
143[package]
144# ...
145description = "A short description of my package"
146```
147
148> **Note**: [crates.io] requires the `description` to be set.
149
6e08c973 150<a id="the-documentation-field-optional"></a>
2a4aa420
EH
151#### The `documentation` field
152
153The `documentation` field specifies a URL to a website hosting the crate's
154documentation. If no URL is specified in the manifest file, [crates.io] will
155automatically link your crate to the corresponding [docs.rs] page.
156
157```toml
158[package]
159# ...
160documentation = "https://docs.rs/bitflags"
161```
162
2a4aa420
EH
163#### The `readme` field
164
165The `readme` field should be the path to a file in the package root (relative
166to this `Cargo.toml`) that contains general information about the package.
167This file will be transferred to the registry when you publish. [crates.io]
168will interpret it as Markdown and render it on the crate's page.
169
170```toml
171[package]
172# ...
173readme = "README.md"
174```
175
54b0432f
TV
176If no value is specified for this field, and a file named `README.md`,
177`README.txt` or `README` exists in the package root, then the name of that
178file will be used. You can suppress this behavior by setting this field to
b0a3cc6b
TV
179`false`. If the field is set to `true`, a default value of `README.md` will
180be assumed.
54b0432f 181
2a4aa420
EH
182#### The `homepage` field
183
184The `homepage` field should be a URL to a site that is the home page for your
185package.
186
187```toml
188[package]
189# ...
190homepage = "https://serde.rs/"
191```
192
193#### The `repository` field
194
195The `repository` field should be a URL to the source repository for your
196package.
197
198```toml
199[package]
200# ...
201repository = "https://github.com/rust-lang/cargo/"
202```
203
204#### The `license` and `license-file` fields
205
206The `license` field contains the name of the software license that the package
207is released under. The `license-file` field contains the path to a file
208containing the text of the license (relative to this `Cargo.toml`).
209
210[crates.io] interprets the `license` field as an [SPDX 2.1 license
211expression][spdx-2.1-license-expressions]. The name must be a known license
212from the [SPDX license list 3.6][spdx-license-list-3.6]. Parentheses are not
213currently supported. See the [SPDX site] for more information.
214
215SPDX license expressions support AND and OR operators to combine multiple
216licenses.[^slash]
217
218```toml
219[package]
220# ...
221license = "MIT OR Apache-2.0"
222```
223
224Using `OR` indicates the user may choose either license. Using `AND` indicates
225the user must comply with both licenses simultaneously. The `WITH` operator
226indicates a license with a special exception. Some examples:
227
228* `MIT OR Apache-2.0`
916b392c
JT
229* `LGPL-2.1-only AND MIT AND BSD-2-Clause`
230* `GPL-2.0-or-later WITH Bison-exception-2.2`
2a4aa420
EH
231
232If a package is using a nonstandard license, then the `license-file` field may
233be specified in lieu of the `license` field.
234
235```toml
236[package]
237# ...
238license-file = "LICENSE.txt"
239```
240
241> **Note**: [crates.io] requires either `license` or `license-file` to be set.
242
243[^slash]: Previously multiple licenses could be separated with a `/`, but that
244usage is deprecated.
245
246#### The `keywords` field
247
248The `keywords` field is an array of strings that describe this package. This
249can help when searching for the package on a registry, and you may choose any
250words that would help someone find this crate.
251
252```toml
253[package]
254# ...
255keywords = ["gamedev", "graphics"]
256```
257
258> **Note**: [crates.io] has a maximum of 5 keywords. Each keyword must be
259> ASCII text, start with a letter, and only contain letters, numbers, `_` or
260> `-`, and have at most 20 characters.
261
262#### The `categories` field
263
264The `categories` field is an array of strings of the categories this package
265belongs to.
266
267```toml
268categories = ["command-line-utilities", "development-tools::cargo-plugins"]
269```
270
271> **Note**: [crates.io] has a maximum of 5 categories. Each category should
272> match one of the strings available at <https://crates.io/category_slugs>, and
273> must match exactly.
274
6e08c973 275<a id="the-workspace--field-optional"></a>
2a4aa420
EH
276#### The `workspace` field
277
278The `workspace` field can be used to configure the workspace that this package
279will be a member of. If not specified this will be inferred as the first
f453bed7
EH
280Cargo.toml with `[workspace]` upwards in the filesystem. Setting this is
281useful if the member is not inside a subdirectory of the workspace root.
2a4aa420
EH
282
283```toml
284[package]
285# ...
286workspace = "path/to/workspace/root"
287```
288
f453bed7
EH
289This field cannot be specified if the manifest already has a `[workspace]`
290table defined. That is, a crate cannot both be a root crate in a workspace
291(contain `[workspace]`) and also be a member crate of another workspace
292(contain `package.workspace`).
293
294For more information, see the [workspaces chapter](workspaces.md).
2a4aa420 295
e267b262 296<a id="package-build"></a>
6e08c973 297<a id="the-build-field-optional"></a>
2a4aa420 298#### The `build` field
4d590f95 299
2a4aa420
EH
300The `build` field specifies a file in the package root which is a [build
301script] for building native code. More information can be found in the [build
302script guide][build script].
4d590f95 303
b119b891 304[build script]: build-scripts.md
4d590f95
BE
305
306```toml
307[package]
308# ...
309build = "build.rs"
310```
311
e267b262
EH
312The default is `"build.rs"`, which loads the script from a file named
313`build.rs` in the root of the package. Use `build = "custom_build_name.rs"` to
314specify a path to a different file or `build = false` to disable automatic
315detection of the build script.
316
6e08c973 317<a id="the-links-field-optional"></a>
2a4aa420 318#### The `links` field
3fb15acf 319
2a4aa420
EH
320The `links` field specifies the name of a native library that is being linked
321to. More information can be found in the [`links`][links] section of the build
3fb15acf
DW
322script guide.
323
b119b891 324[links]: build-scripts.md#the-links-manifest-key
3fb15acf
DW
325
326```toml
327[package]
328# ...
329links = "foo"
3fb15acf
DW
330```
331
6e08c973 332<a id="the-exclude-and-include-fields-optional"></a>
2a4aa420 333#### The `exclude` and `include` fields
4d590f95 334
d4b6e90f
EH
335You can explicitly specify that a set of file patterns should be ignored or
336included for the purposes of packaging. The patterns specified in the
337`exclude` field identify a set of files that are not included, and the
338patterns in `include` specify files that are explicitly included.
339
340The patterns should be [gitignore]-style patterns. Briefly:
341
342- `foo` matches any file or directory with the name `foo` anywhere in the
343 package. This is equivalent to the pattern `**/foo`.
344- `/foo` matches any file or directory with the name `foo` only in the root of
345 the package.
346- `foo/` matches any *directory* with the name `foo` anywhere in the package.
347- Common glob patterns like `*`, `?`, and `[]` are supported:
348 - `*` matches zero or more characters except `/`. For example, `*.html`
349 matches any file or directory with the `.html` extension anywhere in the
350 package.
351 - `?` matches any character except `/`. For example, `foo?` matches `food`,
352 but not `foo`.
353 - `[]` allows for matching a range of characters. For example, `[ab]`
354 matches either `a` or `b`. `[a-z]` matches letters a through z.
355- `**/` prefix matches in any directory. For example, `**/foo/bar` matches the
356 file or directory `bar` anywhere that is directly under directory `foo`.
357- `/**` suffix matches everything inside. For example, `foo/**` matches all
358 files inside directory `foo`, including all files in subdirectories below
359 `foo`.
360- `/**/` matches zero or more directories. For example, `a/**/b` matches
361 `a/b`, `a/x/b`, `a/x/y/b`, and so on.
3ca96e90
EH
362- `!` prefix negates a pattern. For example, a pattern of `src/**.rs` and
363 `!foo.rs` would match all files with the `.rs` extension inside the `src`
364 directory, except for any file named `foo.rs`.
d4b6e90f
EH
365
366If git is being used for a package, the `exclude` field will be seeded with
367the `gitignore` settings from the repository.
4d590f95
BE
368
369```toml
370[package]
371# ...
372exclude = ["build/**/*.o", "doc/**/*.html"]
373```
374
375```toml
376[package]
377# ...
378include = ["src/**/*", "Cargo.toml"]
379```
380
381The options are mutually exclusive: setting `include` will override an
382`exclude`. Note that `include` must be an exhaustive list of files as otherwise
49e37f80
EH
383necessary source files may not be included. The package's `Cargo.toml` is
384automatically included.
4d590f95 385
d4b6e90f
EH
386The include/exclude list is also used for change tracking in some situations.
387For targets built with `rustdoc`, it is used to determine the list of files to
388track to determine if the target should be rebuilt. If the package has a
389[build script] that does not emit any `rerun-if-*` directives, then the
390include/exclude list is used for tracking if the build script should be re-run
391if any of those files change.
4d590f95 392
d4b6e90f 393[gitignore]: https://git-scm.com/docs/gitignore
4d590f95 394
6e08c973 395<a id="the-publish--field-optional"></a>
2a4aa420 396#### The `publish` field
4d590f95
BE
397
398The `publish` field can be used to prevent a package from being published to a
467f878f
DW
399package registry (like *crates.io*) by mistake, for instance to keep a package
400private in a company.
4d590f95
BE
401
402```toml
403[package]
404# ...
405publish = false
406```
407
809486bd 408The value may also be an array of strings which are registry names that are
737382d7
EH
409allowed to be published to.
410
411```toml
412[package]
413# ...
414publish = ["some-registry-name"]
415```
416
81ecfe94
NCA
417If publish array contains a single registry, `cargo publish` command will use
418it when `--registry` flag is not specified.
419
6e08c973 420<a id="the-metadata-table-optional"></a>
543e3817
EH
421#### The `metadata` table
422
423Cargo by default will warn about unused keys in `Cargo.toml` to assist in
424detecting typos and such. The `package.metadata` table, however, is completely
425ignored by Cargo and will not be warned about. This section can be used for
426tools which would like to store package configuration in `Cargo.toml`. For
427example:
428
429```toml
430[package]
431name = "..."
432# ...
433
434# Metadata used when generating an Android APK, for example.
435[package.metadata.android]
436package-name = "my-awesome-android-app"
437assets = "path/to/static"
438```
439
341d4162
BC
440There is a similar table at the workspace level at
441[`workspace.metadata`][workspace-metadata]. While cargo does not specify a
442format for the content of either of these tables, it is suggested that
443external tools may wish to use them in a consistent fashion, such as referring
444to the data in `workspace.metadata` if data is missing from `package.metadata`,
445if that makes sense for the tool in question.
446
16f3b8dd 447[workspace-metadata]: workspaces.md#the-workspacemetadata-table
341d4162 448
543e3817
EH
449#### The `default-run` field
450
451The `default-run` field in the `[package]` section of the manifest can be used
452to specify a default binary picked by [`cargo run`]. For example, when there is
453both `src/bin/a.rs` and `src/bin/b.rs`:
454
455```toml
456[package]
457default-run = "a"
458```
459
80e37c31
DP
460### The `[badges]` section
461
60779a00
EH
462The `[badges]` section is for specifying status badges that can be displayed
463on a registry website when the package is published.
80e37c31 464
60779a00
EH
465> Note: [crates.io] previously displayed badges next to a crate on its
466> website, but that functionality has been removed. Packages should place
467> badges in its README file which will be displayed on [crates.io] (see [the
468> `readme` field](#the-readme-field)).
4d590f95 469
80e37c31 470```toml
4d590f95 471[badges]
60779a00
EH
472# The `maintenance` table indicates the status of the maintenance of
473# the crate. This may be used by a registry, but is currently not
474# used by crates.io. See https://github.com/rust-lang/crates.io/issues/2437
475# and https://github.com/rust-lang/crates.io/issues/2438 for more details.
476#
477# The `status` field is required. Available options are:
dee137b6 478# - `actively-developed`: New features are being added and bugs are being fixed.
479# - `passively-maintained`: There are no plans for new features, but the maintainer intends to
480# respond to issues that get filed.
481# - `as-is`: The crate is feature complete, the maintainer does not intend to continue working on
482# it or providing support, but it works for the purposes it was designed for.
483# - `experimental`: The author wants to share it with the community but is not intending to meet
484# anyone's particular use case.
485# - `looking-for-maintainer`: The current maintainer would like to transfer the crate to someone
486# else.
487# - `deprecated`: The maintainer does not recommend using this crate (the description of the crate
488# can describe why, there could be a better solution available or there could be problems with
489# the crate that the author does not want to fix).
490# - `none`: Displays no badge on crates.io, since the maintainer has not chosen to specify
491# their intentions, potential crate users will need to investigate on their own.
917c3634 492maintenance = { status = "..." }
4d590f95
BE
493```
494
4d590f95
BE
495### Dependency sections
496
b119b891 497See the [specifying dependencies page](specifying-dependencies.md) for
4d590f95
BE
498information on the `[dependencies]`, `[dev-dependencies]`,
499`[build-dependencies]`, and target-specific `[target.*.dependencies]` sections.
500
501### The `[profile.*]` sections
502
dda81d31
EH
503The `[profile]` tables provide a way to customize compiler settings such as
504optimizations and debug settings. See [the Profiles chapter](profiles.md) for
505more detail.
4d590f95 506
4d590f95 507
7dee65fe 508
b119b891
EH
509[`cargo init`]: ../commands/cargo-init.md
510[`cargo new`]: ../commands/cargo-new.md
511[`cargo run`]: ../commands/cargo-run.md
da881b71
EH
512[crates.io]: https://crates.io/
513[docs.rs]: https://docs.rs/
2a4aa420
EH
514[publishing]: publishing.md
515[Rust Edition]: ../../edition-guide/index.html
7dee65fe 516[spdx-2.1-license-expressions]: https://spdx.org/spdx-specification-21-web-version#h.jxpfx0ykyb60
bfc36035 517[spdx-license-list-3.6]: https://github.com/spdx/license-list-data/tree/v3.6
2a4aa420 518[SPDX site]: https://spdx.org/license-list
a133f25c 519[TOML]: https://toml.io/
0a5f54b5
EH
520
521<script>
522(function() {
523 var fragments = {
524 "#the-project-layout": "../guide/project-layout.html",
0f99322f
EH
525 "#examples": "cargo-targets.html#examples",
526 "#tests": "cargo-targets.html#tests",
527 "#integration-tests": "cargo-targets.html#integration-tests",
528 "#configuring-a-target": "cargo-targets.html#configuring-a-target",
529 "#target-auto-discovery": "cargo-targets.html#target-auto-discovery",
6e08c973 530 "#the-required-features-field-optional": "cargo-targets.html#the-required-features-field",
f453bed7
EH
531 "#building-dynamic-or-static-libraries": "cargo-targets.html#the-crate-type-field",
532 "#the-workspace-section": "workspaces.html#the-workspace-section",
533 "#virtual-manifest": "workspaces.html",
dc81356e
EH
534 "#package-selection": "workspaces.html#package-selection",
535 "#the-features-section": "features.html#the-features-section",
d087aeb8
EH
536 "#rules": "features.html",
537 "#usage-in-end-products": "features.html",
538 "#usage-in-packages": "features.html",
d1d08378
EH
539 "#the-patch-section": "overriding-dependencies.html#the-patch-section",
540 "#using-patch-with-multiple-versions": "overriding-dependencies.html#using-patch-with-multiple-versions",
541 "#the-replace-section": "overriding-dependencies.html#the-replace-section",
0a5f54b5
EH
542 };
543 var target = fragments[window.location.hash];
544 if (target) {
545 var url = window.location.toString();
546 var base = url.substring(0, url.lastIndexOf('/'));
547 window.location.replace(base + "/" + target);
548 }
549})();
550</script>