23

Is it possible to specify that a Cargo project requires a minimum rustc version of, for example, 1.1.0 to compile?

telotortium
  • 3,383
  • 2
  • 23
  • 25

6 Answers6

16

In Rust 1.56.0 you can use rust-version:

The rust-version field is an optional key that tells cargo what version of the Rust language and compiler your package can be compiled with. If the currently selected version of the Rust compiler is older than the stated version, cargo will exit with an error, telling the user what version is required.

[package]
rust-version = "1.56"
Stargateur
  • 24,473
  • 8
  • 65
  • 91
  • This is the best for future versions of Rust. If you need to enforce dependencies for rustc before 1.56.0, you'll still need to use the rustc_version crate. – telotortium Aug 30 '22 at 00:33
  • Side question: if crate A depends on crate B and crate B does not build on older versions of rustc, does adding this to crate B will help cargo to select the correct version for crate B when building crate A with an older version of rustc? – Cecile Jan 10 '23 at 09:08
  • 1
    @Cecile Cargo doesn't select rustc version, this can be override by some option or rustup but that doesn't happen automatically, if crates B require a higher rustc version you should have a error message saying your rustc is not up to date when the dependencies is compiled. Then just do a `rustup upgrade`. – Stargateur Jan 10 '23 at 15:01
  • Not selecting the rustc version! Selecting *the latest version of crate B* that can be compiled with the rustc version in use. Maybe it would be easier if I just share the link to the PR, it's hard for me to explain https://github.com/rust-disk-partition-management/gptman/pull/113 – Cecile Jan 11 '23 at 07:40
  • Nevermind, it's clear when reading the doc of rust-version that the only purpose of rust-version is to exit with an error, not give an indicator of which version can be use to compile the crate. Thanks anyway! – Cecile Jan 11 '23 at 07:44
10

You can use a build script like this

extern crate rustc_version;

use std::io::{self, Write};
use std::process::exit;       
use rustc_version::version_matches;   

fn main() {
    if !version_matches(">= 1.1.0") {
        writeln!(&mut io::stderr(), "This crate requires rustc >= 1.1.0.").unwrap();
        exit(1);
    }   
}

This uses the rustc_version crate.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
malbarbo
  • 10,717
  • 1
  • 42
  • 57
  • `rustc_version` 0.2 uses `if version().unwrap() >= Version::parse("1.4.0").unwrap() { println!("cargo:rustc-cfg=compiler_has_important_bugfix"); }` – wegry Jan 18 '19 at 08:22
8

If your project required a minimum rustc version of 1.1.0 to compile, you could simply create a file named rust-toolchain (without any file extension) in the same directory as your Cargo.toml file, and add the following contents to it:

[toolchain]
channel = "1.1.0"
components = ["rust-src"]

Then when you run cargo build it will automatically download and install that version and switch to it. See this Rust Blog post for further details.

This Rust RFC #2495 proposes an alternative approach in future where we may be able to just add the line rust = "1.1.0" to the Cargo.toml file.

Luke Schoen
  • 4,129
  • 2
  • 27
  • 25
  • why another file ? this look like python with hundred of method meh, the rfc should have been finish implemented long time ago. – Stargateur Aug 14 '21 at 13:56
  • This can be used by binaries, but for libraries, it still can't enforce version constraints - that still requires rustc_version. – telotortium Aug 30 '22 at 00:30
4

I've found some old proposals on Github:

https://github.com/rust-lang/cargo/issues/837
https://github.com/rust-lang/cargo/issues/1044
https://github.com/rust-lang/cargo/issues/1214

They were closed with

I think that for now there's not a lot actionable in this ticket, I agree that we'll definitely want to re-evaluate post-1.0, but for now I don't think cargo is going to enter the business of supporting various Rust versions as it's just too unstable to track currently.

So there seems to be no way yet. Maybe you should raise your case there.

Veedrac
  • 58,273
  • 15
  • 112
  • 169
  • 2
    [RFC 2495](https://github.com/rust-lang/rfcs/pull/2495) has been approved for specifying a minimum Rust version, but as of March 2021, it remains [unimplemented](https://github.com/rust-lang/rust/issues/65262) – Austin Mar 08 '21 at 02:25
1

No.

As of right now, the only thing you can realistically do is note the required version in the documentation and/or the README for the crate.

You may be able to configure multirust to use the correct compiler, but keep in mind that it only works in UNIX-y environments.

DK.
  • 55,277
  • 5
  • 189
  • 162
-1

If you use Travis, you can configure which versions of Rust, and which channels, you support. That's a common way to document it.

Steve Klabnik
  • 14,521
  • 4
  • 58
  • 99