-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Description
Problem
As of 1.56 Rust supports a "rust-version" key in Cargo.toml. I love this. I am always nervous about Rust upgrades breaking my code and just last week I hit a confusing error building a project from GitHub, which would have been easy to resolve if the project had used rust-version (but it didn't). I now want to use rust-version in every one of my projects.
However, checking my Rust version and typing it out in the Cargo.toml is a bit obnoxious, and maybe I, a fallable human, will forget the quotation marks or forget if it goes in [package] or some other section or... hm. Cargo does many things for me automatically, such as set the name, version and edition. Cargo should set rust-version for me.
Proposed Solution
In general, Cargo should probably not set rust-version because many programs will incidentally work on versions of Rust older than the one they were initially created with. However, for paranoid folk like me who are making software not for wide distribution, an option should exist to automatically set the rust-version to the current Rust version. This behavior is conservative (it will cause builds to fail on some versions of Rust that might in practice work) but accurate as it is setting the key to the earliest version known to work.
I would expose this in two ways:
- A
cargo update-rust-version
command (or maybecargo bump-rust-version
?) that sets the key automatically for the current project. - A
[cargo new] set-rust-version=true
key in the Cargo configuration file, which if true, causes the key to automatically be set on each instance ofcargo new
.
Notes
Questions I don't have answers to:
Could this be a Cargo plugin?
I find many Cargo plugins in the package registry. If this could be implemented as a Cargo plugin, I would be happy to implement and release that myself. However, looking at the Cargo docs, my understanding is Cargo plugins can only add new cargo commands. Therefore cargo update-rust-version
could be implemented by a Plugin, but the automatic behavior on cargo new
— checking for the config key and adding to the package automatically, which is the more useful feature in the first place— could not. Am I missing something?
What about workspaces?
I dunno
Should there also be a cargo set-rust-version 1.65
command?
I guess??
Could cargo update-rust-version
figure out the minimum working version automatically? (IE scan code to see which features are used?)
That sounds hard, and would catch compile errors but not logic/behavior bugs?