|
| 1 | +## Unstable Features |
| 2 | + |
| 3 | +Experimental Cargo features are only available on the nightly channel. You |
| 4 | +typically use one of the `-Z` flags to enable them. Run `cargo -Z help` to |
| 5 | +see a list of flags available. |
| 6 | + |
| 7 | +`-Z unstable-options` is a generic flag for enabling other unstable |
| 8 | +command-line flags. Options requiring this will be called out below. |
| 9 | + |
| 10 | +Some unstable features will require you to specify the `cargo-features` key in |
| 11 | +`Cargo.toml`. |
| 12 | + |
| 13 | +### Alternate Registries |
| 14 | +* RFC: [#2141](https://github.com/rust-lang/rfcs/blob/master/text/2141-alternative-registries.md) |
| 15 | +* Tracking Issue: [rust-lang/rust#44931](https://github.com/rust-lang/rust/issues/44931) |
| 16 | + |
| 17 | +Alternate registries allow you to use registries other than crates.io. |
| 18 | + |
| 19 | +The name of a registry is defined in `.cargo/config` under the `registries` |
| 20 | +table: |
| 21 | + |
| 22 | +```toml |
| 23 | +[registries] |
| 24 | +my-registry = { index = "https://my-intranet:8080/index" } |
| 25 | +``` |
| 26 | + |
| 27 | +Authentication information for alternate registries can be added to |
| 28 | +`.cargo/credentials`: |
| 29 | + |
| 30 | +```toml |
| 31 | +[my-registry] |
| 32 | +token = "api-token" |
| 33 | +``` |
| 34 | + |
| 35 | +Inside `Cargo.toml` you can specify which registry a dependency comes from |
| 36 | +using the `registry` key. First you need to include the appropriate |
| 37 | +`cargo-features` at the top of the file: |
| 38 | + |
| 39 | +```toml |
| 40 | +cargo-features = ["alternative-registries"] |
| 41 | + |
| 42 | +[package] |
| 43 | +... |
| 44 | + |
| 45 | +[dependencies] |
| 46 | +other-create = { version = "1.0", registry = "my-registry"} |
| 47 | +``` |
| 48 | + |
| 49 | +A `--registry` flag has been added to commands that interact with registries |
| 50 | +such as `publish`, `login`, etc. Example: |
| 51 | + |
| 52 | +``` |
| 53 | +cargo +nightly publish -Z unstable-options --registry my-registry |
| 54 | +``` |
| 55 | + |
| 56 | +The `publish` field in `Cargo.toml` has been extended to accept a list of |
| 57 | +registries that will restrict publishing only to those registries. |
| 58 | + |
| 59 | +```toml |
| 60 | +[package] |
| 61 | +... |
| 62 | +publish = ["my-registry"] |
| 63 | +``` |
| 64 | + |
| 65 | + |
| 66 | +### rename-dependency |
| 67 | +* Original Issue: [#1311](https://github.com/rust-lang/cargo/issues/1311) |
| 68 | +* PR: [#4953](https://github.com/rust-lang/cargo/pull/4953) |
| 69 | + |
| 70 | +The rename-dependency feature allows you to import a dependency |
| 71 | +with a different name from the source. This can be useful in a few scenarios: |
| 72 | + |
| 73 | +* Depending on crates with the same name from different registries. |
| 74 | +* Depending on multiple versions of a crate. |
| 75 | +* Avoid needing `extern crate foo as bar` in Rust source. |
| 76 | + |
| 77 | +Just include the `package` key to specify the actual name of the dependency. |
| 78 | +You must include `cargo-features` at the top of your `Cargo.toml`. |
| 79 | + |
| 80 | +```toml |
| 81 | +cargo-features = ["rename-dependency"] |
| 82 | + |
| 83 | +[package] |
| 84 | +name = "mypackage" |
| 85 | +version = "0.0.1" |
| 86 | + |
| 87 | +[dependencies] |
| 88 | +foo = "0.1" |
| 89 | +bar = { version = "0.1", registry = "custom", package = "foo" } |
| 90 | +baz = { git = "https://github.com/example/project", package = "foo" } |
| 91 | +``` |
| 92 | + |
| 93 | +In this example, three crates are now available in your Rust code: |
| 94 | + |
| 95 | +```rust |
| 96 | +extern crate foo; // crates.io |
| 97 | +extern crate bar; // registry `custom` |
| 98 | +extern crate baz; // git repository |
| 99 | +``` |
| 100 | + |
| 101 | + |
| 102 | +### publish-lockfile |
| 103 | +* Original Issue: [#2263](https://github.com/rust-lang/cargo/issues/2263) |
| 104 | +* PR: [#5093](https://github.com/rust-lang/cargo/pull/5093) |
| 105 | + |
| 106 | +When creating a `.crate` file for distribution, Cargo has historically |
| 107 | +not included the `Cargo.lock` file. This can cause problems with |
| 108 | +using `cargo install` with a binary. You can specify that your package |
| 109 | +should include the `Cargo.lock` file when using `cargo package` or `cargo publish` |
| 110 | +by specifying the `publish-lockfile` key in `Cargo.toml`. This also requires the |
| 111 | +appropriate `cargo-features`: |
| 112 | + |
| 113 | +```toml |
| 114 | +cargo-features = ["publish-lockfile"] |
| 115 | + |
| 116 | +[project] |
| 117 | +... |
| 118 | +publish-lockfile = true |
| 119 | +``` |
| 120 | + |
| 121 | + |
| 122 | +### Offline Mode |
| 123 | +* Original Issue: [#4686](https://github.com/rust-lang/cargo/issues/4686) |
| 124 | + |
| 125 | +The `-Z offline` flag prevents Cargo from attempting to access the network for |
| 126 | +any reason. Typically Cargo will stop with an error if it wants to access the |
| 127 | +network and it is not available. |
| 128 | + |
| 129 | +Beware that this may result in different dependency resolution than online |
| 130 | +mode. Cargo will restrict itself to crates that are available locally, even |
| 131 | +if there might be a newer version as indicated in the local copy of the index. |
| 132 | + |
| 133 | +### no-index-update |
| 134 | +* Original Issue: [#3479](https://github.com/rust-lang/cargo/issues/3479) |
| 135 | + |
| 136 | +The `-Z no-index-update` flag ensures that Cargo does not attempt to update |
| 137 | +the registry index. This is intended for tools such as Crater that issue many |
| 138 | +Cargo commands, and you want to avoid the network latency for updating the |
| 139 | +index each time. |
| 140 | + |
| 141 | +### avoid-dev-deps |
| 142 | +* Original Issue: [#4988](https://github.com/rust-lang/cargo/issues/4988) |
| 143 | +* Stabilization Issue: [#5133](https://github.com/rust-lang/cargo/issues/5133) |
| 144 | + |
| 145 | +When running commands such as `cargo install` or `cargo build`, Cargo |
| 146 | +currently requires dev-dependencies to be downloaded, even if they are not |
| 147 | +used. The `-Z avoid-dev-deps` flag allows Cargo to avoid downloading |
| 148 | +dev-dependencies if they are not needed. The `Cargo.lock` file will not be |
| 149 | +generated if dev-dependencies are skipped. |
| 150 | + |
| 151 | +### minimal-versions |
| 152 | +* Original Issue: [#4100](https://github.com/rust-lang/cargo/issues/4100) |
| 153 | + |
| 154 | +When a `Cargo.lock` file is generated, the `-Z minimal-versions` flag will |
| 155 | +resolve the dependencies to the minimum semver version that will satisfy the |
| 156 | +requirements (instead of the greatest version). |
| 157 | + |
| 158 | +The intended use-case of this flag is to check, during continuous integration, |
| 159 | +that the versions specified in Cargo.toml are a correct reflection of the |
| 160 | +minimum versions that you are actually using. That is, if Cargo.toml says |
| 161 | +`foo = "1.0.0"` that you don't accidentally depend on features added only in |
| 162 | +`foo 1.5.0`. |
| 163 | + |
| 164 | +### out-dir |
| 165 | +* Original Issue: [#4875](https://github.com/rust-lang/cargo/issues/4875) |
| 166 | + |
| 167 | +This feature allows you to specify the directory where artifacts will be |
| 168 | +copied to after they are built. Typically artifacts are only written to the |
| 169 | +`target/release` or `target/debug` directories. However, determining the |
| 170 | +exact filename can be tricky since you need to parse JSON output. The |
| 171 | +`--out-dir` flag makes it easier to predictably access the artifacts. Note |
| 172 | +that the artifacts are copied, so the originals are still in the `target` |
| 173 | +directory. Example: |
| 174 | + |
| 175 | +``` |
| 176 | +cargo +nightly build --out-dir=out -Z unstable-options |
| 177 | +``` |
| 178 | + |
| 179 | + |
| 180 | +### Edition |
| 181 | +* Tracking Issue: [rust-lang/rust#44581](https://github.com/rust-lang/rust/issues/44581) |
| 182 | +* RFC: [#2052](https://github.com/rust-lang/rfcs/blob/master/text/2052-epochs.md) |
| 183 | + |
| 184 | +You can opt in to a specific Rust Edition for your package with the `rust` key |
| 185 | +in `Cargo.toml`. If you don't specify the edition, it will default to 2015. |
| 186 | +You need to include the appropriate `cargo-features`: |
| 187 | + |
| 188 | +```toml |
| 189 | +cargo-features = ["edition"] |
| 190 | + |
| 191 | +[package] |
| 192 | +... |
| 193 | +rust = "2018" |
| 194 | +``` |
0 commit comments