22

How do I set a Cargo project to build & run using nightly by default (i.e. cargo build is actually cargo +nightly build) without setting nightly as the global default?


This is not the same question as How to switch between Rust toolchains. If you read both questions you'll see that question is asking about switching Rust toolchains globally whereas I want to switch Rust toolchains without changing the global setting.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Timmmm
  • 88,195
  • 71
  • 364
  • 509
  • 1
    The proposed duplicate target is not specific to setting a Rust toolchain globally. [This answer](https://stackoverflow.com/a/65644807) proposes `rustup override set`, like in the answer below. And [this one](https://stackoverflow.com/a/64701936) shows that it's better to add a `rust-toolchain` file to the project. – E_net4 Apr 16 '21 at 07:46
  • 1
    "The proposed duplicate target is not specific to setting a Rust toolchain globally." It doesn't specify, so that means globally. "How do I change my desktop background?" and "How do I change my desktop background on one screen only?" are not the same question. – Timmmm Apr 16 '21 at 09:34

2 Answers2

43

With rustup override set nightly it sets the default for that directory to nightly:

https://rust-lang.github.io/rustup/overrides.html#directory-overrides

C14L
  • 12,153
  • 4
  • 39
  • 52
40

If you want this choice to also be reflected in the repository, as opposed to just local configuration, you can add a rust-toolchain.toml file, for example

[toolchain]
channel = "nightly"

You might also want to set the nightly compiler's version (printed by rustup show) in the rust-version field of your cargo.toml, so that dependents which try to build on stable get an error message (since rust-toolchain.toml sets the version for a directory, not a crate).

Dan
  • 12,409
  • 3
  • 50
  • 87