Skip to content

Commit eed6bdc

Browse files
committed
Update documentation for auto-discovery.
The old documentation was slightly wrong, and didn't really go into much detail. This adds some elaboration.
1 parent 7eb9e6c commit eed6bdc

File tree

1 file changed

+44
-7
lines changed

1 file changed

+44
-7
lines changed

src/doc/src/reference/manifest.md

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -648,10 +648,6 @@ Cargo will also treat any files located in `src/bin/*.rs` as executables. If you
648648
executable consists of more than just one source file, you might also use a directory
649649
inside `src/bin` containing a `main.rs` file which will be treated as an executable
650650
with a name of the parent directory.
651-
Do note, however, once you add a `[[bin]]` section ([see
652-
below](#configuring-a-target)), Cargo will no longer automatically build files
653-
located in `src/bin/*.rs`. Instead you must create a `[[bin]]` section for
654-
each file you want to build.
655651

656652
Your package can optionally contain folders named `examples`, `tests`, and
657653
`benches`, which Cargo will treat as containing examples,
@@ -684,6 +680,11 @@ To structure your code after you've created the files and folders for your
684680
package, you should remember to use Rust's module system, which you can read
685681
about in [the book](https://doc.rust-lang.org/book/crates-and-modules.html).
686682

683+
See [Configuring a target](#configuring-a-target) below for more details on
684+
manually configuring target settings. See [Target
685+
auto-discovery](#target-auto-discovery) below for more information on
686+
controlling how Cargo automatically infers targets.
687+
687688
### Examples
688689

689690
Files located under `examples` are example uses of the functionality provided by
@@ -800,9 +801,45 @@ name = "my-cool-binary"
800801
path = "src/my-cool-binary.rs"
801802
```
802803

803-
The `[package]` also includes the optional `autobins`, `autoexamples`,
804-
`autotests`, and `autobenches` keys to explicitly opt-in or opt-out of
805-
auto-discovering specific target kinds.
804+
#### Target auto-discovery
805+
806+
By default, Cargo automatically determines the targets to build based on the
807+
[layout of the files](#the-project-layout) on the filesystem. The target
808+
configuration tables, such as `[lib]`, `[[bin]]`, `[[test]]`, `[[bench]]`, or
809+
`[[example]]`, can be used to add additional targets that don't follow the
810+
standard directory layout.
811+
812+
The automatic target discovery can be disabled so that only manually
813+
configured targets will be built. Setting the keys `autobins`, `autoexamples`,
814+
`autotests`, or `autobenches` to `false` in the `[package]` section will
815+
disable auto-discovery of the corresponding target type.
816+
817+
Disabling automatic discovery should only be needed for specialized
818+
situations. For example, if you have a library where you want a *module* named
819+
`bin`, this would present a problem because Cargo would usually attempt to
820+
compile anything in the `bin` directory as an executable. Here is a sample
821+
layout of this scenario:
822+
823+
```
824+
├── Cargo.toml
825+
└── src
826+
   ├── lib.rs
827+
   └── bin
828+
      └── mod.rs
829+
```
830+
831+
To prevent Cargo from inferring `src/bin/mod.rs` as an executable, set
832+
`autobins = false` in `Cargo.toml` to disable auto-discovery:
833+
834+
```toml
835+
[package]
836+
#
837+
autobins = false
838+
```
839+
840+
> **Note**: For packages with the 2015 edition, the default for auto-discovery
841+
> is `false` if at least one target is manually defined in `Cargo.toml`.
842+
> Beginning with the 2018 edition, the default is always `true`.
806843
807844
#### The `required-features` field (optional)
808845

0 commit comments

Comments
 (0)