HashMap::from([(tuple)]) and Rust 2018

I started a new project with

edition = "2021"

in my Cargo.toml file. I discovered the nifty new feature of HashMap that I could do:

    let vars = HashMap::from([("version", "VERSION: G.0.16")]);

Then I learned that, due to circumstances beyond my control, I need to use Rust 2018. So I changed edition to 2018 and was surprised to see that my code still compiled fine.
Why is that?
I guess I'm confused about the utility of specifying an edition in Cargo.toml.
--wpd

The edition controls language changes (mostly syntax); library additions like the HashMap::from API are unaffected by it.

You can read more about this in the edition guide.

1 Like

Thank you. I was looking at that.
I have an existing build system that uses rustc 1.51, but my development machines have 1.58 on them. I guess I could roll back to 1.51 any time I want to test this code (in order to make sure that I'm not using features that were added with the 2021 edition), but it seems like there should be an easier way.
--wpd

If you want to ensure it builds with Rust 1.51, then you need to use Rust 1.51 – no way around that :smile:

Perhaps consider adding a rust-toolchain.toml. As long as you use rustup, this will automatically pin the Rust version to whatever is in that file.

1 Like

Perfect! That's exactly what I was looking for. Thanks!

--wpd

What I typically see is people making CI do this for them. For example, itertools CI runs both 1.36.0 and stable rust:

2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.