Clarify wildcards in Cargo.toml

Hi there,

I'm a Rust newbie. I've seen in various Cargo.toml files that both .* and .x seem to be common wildcards for dep versions.

The asterisk wildcard is officially blessed by the Cargo docs, and there's no mention of a .x wildcard anywhere. I tried searching both this forum and Github for clarification, but did not succeed.

It seems that using .x is not uncommon, so is it an officially supported version wildcard or not? If it is, I'm happy to update the documentation to reflect this fact.

2 Likes

When looking at those with .x none is simply .toml or they are not in actual versions?

Did you try by yourself to check? Im on a phone.

I do see one for a github branch though.

This one is.

I also briefly tried out using .x in one of my own projects, and it seemed to work.

1 Like

Found some info here https://stackoverflow.com/questions/75642385/what-does-it-mean-and-x-x-in-helm-version

Not for cargo though, but seems to exist just very under documented.

.x doesn't work. It's not a valid syntax.

The search results you've linked to have .x either in comments, or git branch names (not a version), or seem to be templates/placeholders/examples that are not valid as-is.

Keep in mind that in Cargo, version = "1.0.0" allows using v1.9999.9999. The version you specify is a minimum version, and not the version that you will get. In semver terms, there's an implied ^ version range there. Cargo will pick the maximum version that has the same semver-major version number (first non-0 component is the major one in Cargo).

I just created a new package with cargo new, added serde = "1.0.x" to [dependencies], and successfully ran cargo check. Seems to work.

For the record, I just took a look at the code for semver::Comparator's FromStr impl, and it treats x and X as wildcards that are handled the same way as *.

I'm not sure what you mean. The github search link I provided filters by Cargo.toml filepath.

Here is a concrete example. I did confirm this repo builds; cargo build and cargo test both work.

Thank you! I think this is what I was searching for. It looks like the x/X wildcard feature was added 4 years ago. The docs about wildcards were written 6-8 years ago.

(note: had to strip the hyperlinks from jwoder's comment in my quote reply since apparently new users can't have >5 links in one post)

1 Like

I believe keyko is referring to the fact that not all of the results are for files named exactly "Cargo.toml"; I see Cargo.toml.types, Cargo.toml.in, Cargo.toml.old, Cargo.toml.bad, and Cargo.toml.jinja all within the first 10 results. In addition, even among the actual Cargo.toml files, some uses of .x are not in actual versions, such as the first hit, where it's in a Git branch name.

1 Like

Ah I see. Yeah, I had visually confirmed in the first ~few pages that there were more than just a handful of actual .x version wildcards, but didn't bother refining my query further. Sorry for the confusion; I should have just directly linked the Cargo.toml file I was looking at.

1 Like

Just want to clarify that yes, we document * as the wildcard character but using them in version requirements is discouraged, see Specifying Dependencies - The Cargo Book (need to get around to creating an anchor for this...)

1 Like