Skip to content

Commit dc81356

Browse files
committed
Move features to a separate chapter.
1 parent f453bed commit dc81356

File tree

8 files changed

+148
-136
lines changed

8 files changed

+148
-136
lines changed

src/doc/src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
* [The Manifest Format](reference/manifest.md)
2424
* [Cargo Targets](reference/cargo-targets.md)
2525
* [Workspaces](reference/workspaces.md)
26+
* [Features](reference/features.md)
2627
* [Profiles](reference/profiles.md)
2728
* [Configuration](reference/config.md)
2829
* [Environment Variables](reference/environment-variables.md)

src/doc/src/appendix/glossary.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ manifest is located.
182182
[edition guide]: ../../edition-guide/index.html
183183
[edition-field]: ../reference/manifest.md#the-edition-field
184184
[environment variable]: ../reference/environment-variables.md
185-
[feature]: ../reference/manifest.md#the-features-section
185+
[feature]: ../reference/features.md
186186
[git dependency]: ../reference/specifying-dependencies.md#specifying-dependencies-from-git-repositories
187187
[git source]: ../reference/source-replacement.md
188188
[integration-tests]: ../reference/cargo-targets.md#integration-tests

src/doc/src/reference/build-scripts.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ of `feature=`). Or an arbitrary key/value pair may be used with an `=` symbol
176176
like `cargo:rustc-cfg=my_component="foo"`. The key should be a Rust
177177
identifier, the value should be a string.
178178

179-
[cargo features]: manifest.md#the-features-section
179+
[cargo features]: features.md
180180
[conditional compilation]: ../../reference/conditional-compilation.md
181181
[option-cfg]: ../../rustc/command-line-arguments.md#option-cfg
182182

src/doc/src/reference/cargo-targets.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ autobins = false
359359
[dependencies]: specifying-dependencies.md
360360
[dev-dependencies]: specifying-dependencies.md#development-dependencies
361361
[documentation examples]: ../../rustdoc/documentation-tests.html
362-
[features]: manifest.md#the-features-section
362+
[features]: features.md
363363
[nightly channel]: ../../book/appendix-07-nightly-rust.html
364364
[package layout]: ../guide/project-layout.md
365365
[package-edition]: manifest.md#the-edition-field

src/doc/src/reference/features.md

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
## Features
2+
3+
Cargo supports features to allow expression of:
4+
5+
* conditional compilation options (usable through `cfg` attributes);
6+
* optional dependencies, which enhance a package, but are not required; and
7+
* clusters of optional dependencies, such as `postgres-all`, that would include the
8+
`postgres` package, the `postgres-macros` package, and possibly other packages
9+
(such as development-time mocking libraries, debugging tools, etc.).
10+
11+
A feature of a package is either an optional dependency, or a set of other
12+
features.
13+
14+
### The `[features]` section
15+
16+
Features are defined in the `[features]` table of `Cargo.toml`. The format for
17+
specifying features is:
18+
19+
```toml
20+
[package]
21+
name = "awesome"
22+
23+
[features]
24+
# The default set of optional packages. Most people will want to use these
25+
# packages, but they are strictly optional. Note that `session` is not a package
26+
# but rather another feature listed in this manifest.
27+
default = ["jquery", "uglifier", "session"]
28+
29+
# A feature with no dependencies is used mainly for conditional compilation,
30+
# like `#[cfg(feature = "go-faster")]`.
31+
go-faster = []
32+
33+
# The `secure-password` feature depends on the bcrypt package. This aliasing
34+
# will allow people to talk about the feature in a higher-level way and allow
35+
# this package to add more requirements to the feature in the future.
36+
secure-password = ["bcrypt"]
37+
38+
# Features can be used to reexport features of other packages. The `session`
39+
# feature of package `awesome` will ensure that the `session` feature of the
40+
# package `cookie` is also enabled.
41+
session = ["cookie/session"]
42+
43+
[dependencies]
44+
# These packages are mandatory and form the core of this package’s distribution.
45+
cookie = "1.2.0"
46+
oauth = "1.1.0"
47+
route-recognizer = "=2.1.0"
48+
49+
# A list of all of the optional dependencies, some of which are included in the
50+
# above `features`. They can be opted into by apps.
51+
jquery = { version = "1.0.2", optional = true }
52+
uglifier = { version = "1.5.3", optional = true }
53+
bcrypt = { version = "*", optional = true }
54+
civet = { version = "*", optional = true }
55+
```
56+
57+
To use the package `awesome`:
58+
59+
```toml
60+
[dependencies.awesome]
61+
version = "1.3.5"
62+
default-features = false # do not include the default features, and optionally
63+
# cherry-pick individual features
64+
features = ["secure-password", "civet"]
65+
```
66+
67+
### Rules
68+
69+
The usage of features is subject to a few rules:
70+
71+
* Feature names must not conflict with other package names in the manifest. This
72+
is because they are opted into via `features = [...]`, which only has a single
73+
namespace.
74+
* With the exception of the `default` feature, all features are opt-in. To opt
75+
out of the default feature, use `default-features = false` and cherry-pick
76+
individual features.
77+
* Feature groups are not allowed to cyclically depend on one another.
78+
* Dev-dependencies cannot be optional.
79+
* Features groups can only reference optional dependencies.
80+
* When a feature is selected, Cargo will call `rustc` with `--cfg
81+
feature="${feature_name}"`. If a feature group is included, it and all of its
82+
individual features will be included. This can be tested in code via
83+
`#[cfg(feature = "foo")]`.
84+
85+
Note that it is explicitly allowed for features to not actually activate any
86+
optional dependencies. This allows packages to internally enable/disable
87+
features without requiring a new dependency.
88+
89+
> **Note**: [crates.io] requires feature names to only contain ASCII letters,
90+
> digits, `_`, or `-`.
91+
92+
### Usage in end products
93+
94+
One major use-case for this feature is specifying optional features in
95+
end-products. For example, the Servo package may want to include optional
96+
features that people can enable or disable when they build it.
97+
98+
In that case, Servo will describe features in its `Cargo.toml` and they can be
99+
enabled using command-line flags:
100+
101+
```console
102+
$ cargo build --release --features "shumway pdf"
103+
```
104+
105+
Default features could be excluded using `--no-default-features`.
106+
107+
### Usage in packages
108+
109+
In most cases, the concept of *optional dependency* in a library is best
110+
expressed as a separate package that the top-level application depends on.
111+
112+
However, high-level packages, like Iron or Piston, may want the ability to
113+
curate a number of packages for easy installation. The current Cargo system
114+
allows them to curate a number of mandatory dependencies into a single package
115+
for easy installation.
116+
117+
In some cases, packages may want to provide additional curation for optional
118+
dependencies:
119+
120+
* grouping a number of low-level optional dependencies together into a single
121+
high-level feature;
122+
* specifying packages that are recommended (or suggested) to be included by
123+
users of the package; and
124+
* including a feature (like `secure-password` in the motivating example) that
125+
will only work if an optional dependency is available, and would be difficult
126+
to implement as a separate package (for example, it may be overly difficult to
127+
design an IO package to be completely decoupled from OpenSSL, with opt-in via
128+
the inclusion of a separate package).
129+
130+
In almost all cases, it is an antipattern to use these features outside of
131+
high-level packages that are designed for curation. If a feature is optional, it
132+
can almost certainly be expressed as a separate package.
133+
134+
[crates.io]: https://crates.io/

src/doc/src/reference/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ The reference covers the details of various areas of Cargo.
66
* [The Manifest Format](manifest.md)
77
* [Cargo Targets](cargo-targets.md)
88
* [Workspaces](workspaces.md)
9+
* [Features](features.md)
910
* [Profiles](profiles.md)
1011
* [Configuration](config.md)
1112
* [Environment Variables](environment-variables.md)

src/doc/src/reference/manifest.md

Lines changed: 5 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -472,134 +472,6 @@ The `[profile]` tables provide a way to customize compiler settings such as
472472
optimizations and debug settings. See [the Profiles chapter](profiles.md) for
473473
more detail.
474474

475-
### The `[features]` section
476-
477-
Cargo supports features to allow expression of:
478-
479-
* conditional compilation options (usable through `cfg` attributes);
480-
* optional dependencies, which enhance a package, but are not required; and
481-
* clusters of optional dependencies, such as `postgres-all`, that would include the
482-
`postgres` package, the `postgres-macros` package, and possibly other packages
483-
(such as development-time mocking libraries, debugging tools, etc.).
484-
485-
A feature of a package is either an optional dependency, or a set of other
486-
features. The format for specifying features is:
487-
488-
```toml
489-
[package]
490-
name = "awesome"
491-
492-
[features]
493-
# The default set of optional packages. Most people will want to use these
494-
# packages, but they are strictly optional. Note that `session` is not a package
495-
# but rather another feature listed in this manifest.
496-
default = ["jquery", "uglifier", "session"]
497-
498-
# A feature with no dependencies is used mainly for conditional compilation,
499-
# like `#[cfg(feature = "go-faster")]`.
500-
go-faster = []
501-
502-
# The `secure-password` feature depends on the bcrypt package. This aliasing
503-
# will allow people to talk about the feature in a higher-level way and allow
504-
# this package to add more requirements to the feature in the future.
505-
secure-password = ["bcrypt"]
506-
507-
# Features can be used to reexport features of other packages. The `session`
508-
# feature of package `awesome` will ensure that the `session` feature of the
509-
# package `cookie` is also enabled.
510-
session = ["cookie/session"]
511-
512-
[dependencies]
513-
# These packages are mandatory and form the core of this package’s distribution.
514-
cookie = "1.2.0"
515-
oauth = "1.1.0"
516-
route-recognizer = "=2.1.0"
517-
518-
# A list of all of the optional dependencies, some of which are included in the
519-
# above `features`. They can be opted into by apps.
520-
jquery = { version = "1.0.2", optional = true }
521-
uglifier = { version = "1.5.3", optional = true }
522-
bcrypt = { version = "*", optional = true }
523-
civet = { version = "*", optional = true }
524-
```
525-
526-
To use the package `awesome`:
527-
528-
```toml
529-
[dependencies.awesome]
530-
version = "1.3.5"
531-
default-features = false # do not include the default features, and optionally
532-
# cherry-pick individual features
533-
features = ["secure-password", "civet"]
534-
```
535-
536-
#### Rules
537-
538-
The usage of features is subject to a few rules:
539-
540-
* Feature names must not conflict with other package names in the manifest. This
541-
is because they are opted into via `features = [...]`, which only has a single
542-
namespace.
543-
* With the exception of the `default` feature, all features are opt-in. To opt
544-
out of the default feature, use `default-features = false` and cherry-pick
545-
individual features.
546-
* Feature groups are not allowed to cyclically depend on one another.
547-
* Dev-dependencies cannot be optional.
548-
* Features groups can only reference optional dependencies.
549-
* When a feature is selected, Cargo will call `rustc` with `--cfg
550-
feature="${feature_name}"`. If a feature group is included, it and all of its
551-
individual features will be included. This can be tested in code via
552-
`#[cfg(feature = "foo")]`.
553-
554-
Note that it is explicitly allowed for features to not actually activate any
555-
optional dependencies. This allows packages to internally enable/disable
556-
features without requiring a new dependency.
557-
558-
> **Note**: [crates.io] requires feature names to only contain ASCII letters,
559-
> digits, `_`, or `-`.
560-
561-
#### Usage in end products
562-
563-
One major use-case for this feature is specifying optional features in
564-
end-products. For example, the Servo package may want to include optional
565-
features that people can enable or disable when they build it.
566-
567-
In that case, Servo will describe features in its `Cargo.toml` and they can be
568-
enabled using command-line flags:
569-
570-
```console
571-
$ cargo build --release --features "shumway pdf"
572-
```
573-
574-
Default features could be excluded using `--no-default-features`.
575-
576-
#### Usage in packages
577-
578-
In most cases, the concept of *optional dependency* in a library is best
579-
expressed as a separate package that the top-level application depends on.
580-
581-
However, high-level packages, like Iron or Piston, may want the ability to
582-
curate a number of packages for easy installation. The current Cargo system
583-
allows them to curate a number of mandatory dependencies into a single package
584-
for easy installation.
585-
586-
In some cases, packages may want to provide additional curation for optional
587-
dependencies:
588-
589-
* grouping a number of low-level optional dependencies together into a single
590-
high-level feature;
591-
* specifying packages that are recommended (or suggested) to be included by
592-
users of the package; and
593-
* including a feature (like `secure-password` in the motivating example) that
594-
will only work if an optional dependency is available, and would be difficult
595-
to implement as a separate package (for example, it may be overly difficult to
596-
design an IO package to be completely decoupled from OpenSSL, with opt-in via
597-
the inclusion of a separate package).
598-
599-
In almost all cases, it is an antipattern to use these features outside of
600-
high-level packages that are designed for curation. If a feature is optional, it
601-
can almost certainly be expressed as a separate package.
602-
603475
### The `[patch]` Section
604476

605477
This section of Cargo.toml can be used to [override dependencies][replace] with
@@ -717,7 +589,11 @@ dependencies][replace] section of the documentation.
717589
"#building-dynamic-or-static-libraries": "cargo-targets.html#the-crate-type-field",
718590
"#the-workspace-section": "workspaces.html#the-workspace-section",
719591
"#virtual-manifest": "workspaces.html",
720-
"#package-selection": "workspaces.html#package-selection"
592+
"#package-selection": "workspaces.html#package-selection",
593+
"#the-features-section": "features.html#the-features-section",
594+
"#rules": "features.html#rules",
595+
"#usage-in-end-products": "features.html#usage-in-end-products",
596+
"#usage-in-packages": "features.html#usage-in-packages",
721597
};
722598
var target = fragments[window.location.hash];
723599
if (target) {

src/doc/src/reference/specifying-dependencies.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -510,8 +510,8 @@ run `rustc --print=cfg --target=x86_64-pc-windows-msvc`.
510510

511511
Unlike in your Rust source code, you cannot use
512512
`[target.'cfg(feature = "fancy-feature")'.dependencies]` to add dependencies
513-
based on optional features. Use [the `[features]`
514-
section](manifest.md#the-features-section) instead:
513+
based on optional features. Use [the `[features]` section](features.md)
514+
instead:
515515

516516
```toml
517517
[dependencies]
@@ -618,8 +618,8 @@ default-features = false # do not include the default features, and optionally
618618
features = ["secure-password", "civet"]
619619
```
620620

621-
More information about features can be found in the
622-
[manifest documentation](manifest.md#the-features-section).
621+
More information about features can be found in the [features
622+
chapter](features.md).
623623

624624
### Renaming dependencies in `Cargo.toml`
625625

0 commit comments

Comments
 (0)