15
votes

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

5

5 Answers

9
votes

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.

5
votes

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.

4
votes

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.

1
votes

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.

-1
votes

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