@@ -472,134 +472,6 @@ The `[profile]` tables provide a way to customize compiler settings such as
472
472
optimizations and debug settings. See [ the Profiles chapter] ( profiles.md ) for
473
473
more detail.
474
474
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
-
603
475
### The ` [patch] ` Section
604
476
605
477
This section of Cargo.toml can be used to [ override dependencies] [ replace ] with
@@ -717,7 +589,11 @@ dependencies][replace] section of the documentation.
717
589
" #building-dynamic-or-static-libraries" : " cargo-targets.html#the-crate-type-field" ,
718
590
" #the-workspace-section" : " workspaces.html#the-workspace-section" ,
719
591
" #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" ,
721
597
};
722
598
var target = fragments[window .location .hash ];
723
599
if (target) {
0 commit comments