How to compile?

The pros and cons of compiling via cargo or rustc ?

I have current rustc, cargo, rustup. when I create a toml file the edition is 2018. is that ok ?

You should use Cargo to build your Rust projects. You can call rustc directly for simple projects with no dependencies, but it's more straightforward to always use Cargo.

  • pros of using Cargo: it makes building your project simple, supports resolving, fetching, and updating dependencies, pushes you to use a standard project layout, allows publishing on, and is unlikely to cause confusion to other Rust programmers reading your code
  • pros of using rustc directly: if your project has very unusual requirements that Cargo doesn't support, or if you need to integrate with a different build system inside a larger, multi-language project, then calling rustc directly gives you additional flexibility

Yes, edition 2018 is the current edition and is what you should be using for new code. Very soon edition 2021 will be released, and at that point you can change that line in Cargo.toml to read edition = "2021".

1 Like

how do you rustacians know when a new edition has been released?
do we have to download anything in relation to the new edition ?

Well, it happens infrequently—about every three years so far. Any upcoming editions will be announced on, among other places. Note that even when a new edition is released, Rust guarantees that crates using previous editions will keep compiling and can interoperate with crates using the new edition. So moving to a new edition is not a time-sensitive thing.

You just have to upgrade your toolchain with rustup update.

See the Edition Guide for more detail on all this stuff.

so, you need to know what edition will cover whatever version is installed?

Whatever version of what?


  • edition 2015 is supported by Rust version 1.0 and up
  • edition 2018 is supported by Rust version 1.31 and up
  • edition 2021 (not yet released) is/will be supported by Rust version 1.56 and up

Does that make sense? Generally when you're writing new code you should always put the most recent edition in Cargo.toml, and then just run rustup update regularly (maybe a couple times a month) so you're always using an up-to-date toolchain. If you do that then you'll be good to go as far as your own (locally-developed) crates are concerned, and you don't have to worry about what editions other people's crates use.

**yeah, thats what i was getting at. **
so, just keep an eye out for new versions of rust, new editions, add the edition to the toml file, and update rustup

Is there a downside to staying up to date with everything?
backward compatibility could be a issue

Rust generally guarantees that code that works with one version of the compiler will continue to work with all future versions. The exceptions to this are:

  • Bug fixes: if your code only compiled because of a serious compiler bug, new versions may include fixes that make it stop compiling. This happens rarely and when it does it's typically because the code was already broken. More about the bug fix policy.
  • Editions: new editions may include changes to the language that cause code written against previous editions to stop compiling. For example, in edition 2018 Rust switched to a new implementation of the borrow check, called "non-lexical lifetimes" or NLL, and this new borrow check rejected some programs that were previously accepted. (Many of these programs had memory safety bugs that were simply overlooked by the compiler before.) Editions can also do things like introduce new keywords and ban deprecated syntax. Rust has policies that sharply limit the kind of breaking changes that are allowed in editions, which you can read about in the Edition Guide (linked in my previous post). Also, as mentioned before, new versions of the compiler continue to support old editions, so you don't have to move your code to a new edition unless/until you want to.

Overall, Rust takes backward compatibility very seriously and tries to break it only in rare and extremely well-justified situations; it's one of the guiding principles of the whole project.

(This all assumes you're using the stable channel. If you use the beta or nightly channel then breaking changes will happen more regularly.)

1 Like

stable channel, you mean for the rust version ?

The stable channel means you get each new Rust version as it's released. On the beta channel, you get a new Rust version when the previous version is released, so six weeks in advance. On the nightly channel, you get whatever's in the master branch of the Rust repository, updated every day-ish. More on the channels and the release model.

If you installed everything the normal way with rustup, you are on the stable channel by default.

1 Like

ok. i think i am clear on the things that were on my mind:
use cargo to compile, wait for the new edition and then update toml, use rustup to keep rustc and cargo updated, put /target folder in a .gitignore file.

1 Like

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.