22

I am creating a library with Rust. On library creation I type

cargo new name

According to the docs this should create a lib, because --bin is omitted. However, the file is auto set to a binary.

Is there a setting I have to adjust to disable auto setting all projects to binary?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
seamus
  • 2,681
  • 7
  • 26
  • 49

2 Answers2

33

Cargo features

Cargo’s CLI has one really important change this release: cargo new will now default to generating a binary, rather than a library. We try to keep Cargo’s CLI quite stable, but this change is important, and is unlikely to cause breakage.

For some background, cargo new accepts two flags: --lib, for creating libraries, and --bin, for creating binaries, or executables. If you don’t pass one of these flags, in previous versions of Cargo, it would default to --lib. We made this decision because each binary (often) depends on many libraries, and so the library case is more common. However, this is incorrect; each library is depended upon by many binaries. Furthermore, when getting started, what you often want is a program you can run and play around with. It’s not just new Rustaceans though; even very long-time community members have said that they find this default surprising. As such, we’re changing it.

Source

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Stargateur
  • 24,473
  • 8
  • 65
  • 91
  • Derp. Thanks. Will mark as answered after time limit is up – seamus Apr 07 '18 at 10:50
  • 2
    Ouch -- everyone who found this surprising and had to learn the Rust way now has to unlearn that -- every piece of documentation written since then is now wrong, my Kindle ebook "Programming Rust" is wrong, and so are all the printed copies of it. Wow. Rust is not a baby language anymore, it has real users. This seems like a really strange change to make at this stage. – Fredrick Brennan Apr 07 '18 at 11:27
  • 1
    @FredrickBrennan I have checked and all calls in "Programming Rust" are `cargo new --bin` which is as it was, what have changed is that `cargo new` (without any flags) will now behave different. If you have been using flags always then nothing will change. – Hauleth Apr 07 '18 at 11:36
  • @FredrickBrennan This concern cargo not rust directly. – Stargateur Apr 07 '18 at 11:37
6

Since Cargo 1.25 cargo new defaults to creating a binary crate, instead of a library crate.

cargo new accepts two flags: --lib, for creating libraries, and --bin, for creating binaries, or executables.

See the Changelog for 1.25.

Tim Diekmann
  • 7,755
  • 11
  • 41
  • 69