Search code examples
rustrustup

How to switch between Rust toolchains?


rustup help toolchain lists the following sub-commands

SUBCOMMANDS:
    list         List installed toolchains
    install      Install or update a given toolchain
    uninstall    Uninstall a toolchain
    link         Create a custom toolchain by symlinking to a directory
    help         Prints this message or the help of the given subcommand(s)

I have the following toolchains installed

stable-x86_64-unknown-linux-gnu (default)
nightly-2019-09-05-x86_64-unknown-linux-gnu
nightly-x86_64-unknown-linux-gnu
master

I was trying to solve an issue for rust-clippy so I had to install the master toolchain. Even though stable is set as my default toolchain, my current toolchain is master and I would like to switch back to stable. How do I do it without uninstalling the master toolchain?

Is there no switch subcommand?


Solution

  • Of course the rustup default stable command works well, but the easiest way is to keep a rust-toolchain file inside your project root folder. Similar to how you'd keep a .nvm file for a Node.js project.

    Please note that if you use rust-toolchain.toml instead of just rust-toolchain you will need to add needed sections similar to JSON ref and hence I prefer just the simple rust-toolchain file which doesn't need any sections similar to Node.js's .nvm file.

    Cargo is not concerned with versioning for toolchains, +nightly is handled by a rustup wrapper; hence a separate file. (as noted by @cafce25 below)

    rust-toolchain

    nightly
    

    screenshot of IDE with rust-toolchain file

    or

    stable
    

    Please note you could even add toml like sections to rust-toolchain file.

    example: ./rust-toolchain

    [toolchain]
    channel = "1.72.0"
    components = [ "rustfmt", "clippy" ]
    targets = [ "wasm32-unknown-unknown" ]