Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specify the version of rustc required for a Cargo project

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

like image 266
telotortium Avatar asked Sep 28 '15 11:09

telotortium


2 Answers

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.

like image 163
malbarbo Avatar answered Oct 26 '22 15:10

malbarbo


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.

like image 29
Luke Schoen Avatar answered Oct 26 '22 15:10

Luke Schoen