What is the difference between edition and version in rust?

why do we need edition field in Cargo.toml? why do we have both version (compiler) and rust edition.
In general explain me what are these

Editions are like version numbers for the language. They exist because over time, there is a desire to change things in the language in a non-backward-compatible way. You can't just change them outright, as this would break existing code. Instead, they are changed in the next edition of the Rust language (every 3 years, I think).

But this also means you have to specify which edition you want. Cargo and the compiler can't just assume you want the newest edition, because that would also break existing code. So Cargo.toml has an edition field for specifying which edition of the language you want.

The Rust compiler supports the current and all prior editions of the language. This means if you have code using an older edition, you can still upgrade to and use the latest compiler. This allows the core team to justify not supporting old versions of the compiler, because you can (almost always) just upgrade to the newest one no matter how old your code is.

11 Likes

You may find these docs helpful! What are editions? - The Rust Edition Guide

4 Likes

There is the language specification and there is the compiler that implements it. Often for other language there are more than one compiler implementing it. For example C++ is implemented by GCC and Clang and others.

If the Rust language definition changes that is a language edition change. If the compiler gets changed that is a compiler version change.

So, the compiler can be changed, if only for fixing bugs or improving compilation speed, thus producing a new compiler version, while the language it is compiling is still the same edition.

As far as I understand.

3 Likes

That doesn't seem quite right. New language editions features are frequently added without an edition.

Editions are specifically a mechanism to make carefully considered breaking changes while avoiding the python 2->3 problem (huge ecosystem split)

The compiler will be able to compile rust code written in any edition since 1.0 rust 2015. Always, forever. That's the rust backwards compatibility guarantee.

You can write rust code today in rust 2021 edition that isn't valid rust 2015 edition code, but the latest version of the compiler can compile both, and you can depend on external crates that use any edition your compiler understands. So the 2015 crate can depend on the 2021 crate and vice versa.

The compiler has to know what edition your crate is using in order to compile it, which is why you have the edition field in the cargo.toml.

The version field is the version of your crate (used for dependency selection by other crates).

The compiler version affects which editions are available and which rust language features are available. As well as compilation speed changes, bug fixes and things like that.

The great news is, if you wrote a crate back in 2015 and you love it because it works and does what it needs to do. You'll be able to keep using that crate forever :slight_smile:

4 Likes

This is confusing because it is saying you can add an edition without an edition. I'm guessing you accidentally used the wrong term. By "language editions" did you mean language changes, definitions or features?

3 Likes

He means "version of Rust". As in

"The Rust team is happy to announce a new version of Rust, 1.76.0"

Otherwise it was very accurate I think!

1 Like

Ha whoops! Good spot thank you :+1:

1 Like

Rust edition (like 2021) is like C++ version (C++11, C++17, etc). It guarantees some level of functionality of the language.

Version is a particular compiler (like GCC 12 or Clang 16) which may or may not implement a particular edition.

As today there's only one stable implementation of Rust (rustc), such questions arise. Once there are multiple implementations (like brewing GCC-based one), edition will probably grow more important than it is now.

2 Likes

Dang, if this was StackOverflow I'd be adding a bounty to this answer. :slight_smile: That clarified the situation in one sentence - I had originally been thinking that Editions were basically versions, presumably of the compiler. But now I see that it's actually applying a separation between the language and the compiler.

I do wonder how "crufty" this is going to make the compiler, when the year 2525 rolls around...

1 Like

Editions require surprisingly little overhead in the compiler. Aside from a couple random hacks (that could potentially be generalized in the future), it's mostly a few if/else statements here and there.

6 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.