# Should "cargo new" default to applications?

**URL:** <https://internals.rust-lang.org/t/should-cargo-new-default-to-applications/3772>\
**Category:** cargo\
**Created:** [July 29, 2016, 4:48pm UTC](https://internals.rust-lang.org/t/should-cargo-new-default-to-applications/3772 "2016-07-29T16:48:48Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![jntrnr](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jntrnr/32/3046_2.png) [@jntrnr](https://internals.rust-lang.org/u/jntrnr)\
**Post date:** [July 29, 2016, 4:48pm UTC](https://internals.rust-lang.org/t/should-cargo-new-default-to-applications/3772/1 "2016-07-29T16:48:48Z")

</div>

Currently, if you type “cargo new foo”, it will create a new project template in the ./foo directory. This template, by default, is a library template. When I first saw this, it struck me as a quirky default but didn’t pay all that much mind.

Fast-forward, and we’re teaching new users over and over via the book and materials to always pass --bin when they start learning cargo. I got to wondering if we possibly should change the default. How often are people creating a binary vs creating a library? Shouldn’t the default be the common case?

I did a quick browse at the survey data also to see, and ~550 of the roughly 2000 active Rust developers are contributing to [crates.io](http://crates.io). This seems to be one data point in favor of changing the default.

Sounded like a good question for a Rust internals thread. How often do you cargo new/init a new application vs a new library? Should we change the default?

---

<div class="post-metadata">

**Author:** ![eminence](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/eminence/32/4018_2.png) [@eminence](https://internals.rust-lang.org/u/eminence)\
**Post date:** [July 29, 2016, 5:19pm UTC](https://internals.rust-lang.org/t/should-cargo-new-default-to-applications/3772/2 "2016-07-29T17:19:05Z")

</div>

A common[1] pattern is to create a library crate, but give it a [main.rs](http://main.rs) which will use the library to do stuff (things like command line processing go into [main.rs](http://main.rs), and the core functionality is put into [lib.rs](http://lib.rs)). I personally like this approach because if I ever want to use the functionality I just wrote in another project, it’s already written as a library.

[1] Citation Needed. I don’t actually know how common this is, but at least anecdotally it seems to be a recommended practice.

---

<div class="post-metadata">

**Author:** ![matklad](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/matklad/32/12266_2.png) [@matklad](https://internals.rust-lang.org/u/matklad)\
**Post date:** [July 29, 2016, 5:35pm UTC](https://internals.rust-lang.org/t/should-cargo-new-default-to-applications/3772/3 "2016-07-29T17:35:33Z")

</div>

I’ve added a “Create new Cargo project” feature to IntelliJ Rust, and it uses `--bin` flag (not customizable at the moment ;( )

---

<div class="post-metadata">

**Author:** ![withoutboats](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/withoutboats/32/4560_2.png) [@withoutboats](https://internals.rust-lang.org/u/withoutboats)\
**Post date:** [July 29, 2016, 6:15pm UTC](https://internals.rust-lang.org/t/should-cargo-new-default-to-applications/3772/4 "2016-07-29T18:15:35Z")

</div>

I am usually creating a lib, but I think its more important what very new users are most likely to create. We could also turn these from flags to just another set of subcommands and make them mandatory, as in `cargo new lib` and `cargo new app`.

This could open cargo up to have user defined crate templates for initializing projects with certain dependencies, module structure, boilerplate, etc.

---

<div class="post-metadata">

**Author:** ![cswords](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cswords/32/1682_2.png) [@cswords](https://internals.rust-lang.org/u/cswords)\
**Post date:** [July 29, 2016, 6:17pm UTC](https://internals.rust-lang.org/t/should-cargo-new-default-to-applications/3772/5 "2016-07-29T18:17:27Z")

</div>

In my experience, I’m usually after `--bin` if I’m starting a project to _do something_. Furthermore, when I want a go about building a library, I am already thinking about the library and its structure, so the mental overhead of `cargo new --lib` seems smaller.

edit: I think user-defined templates sounds awesome! `cargo new webapp`, `cargo new asmapp`, `cargo new macrolib`, `cargo new dbwebapp`, the list goes on!

---

<div class="post-metadata">

**Author:** ![eminence](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/eminence/32/4018_2.png) [@eminence](https://internals.rust-lang.org/u/eminence)\
**Post date:** [July 29, 2016, 6:41pm UTC](https://internals.rust-lang.org/t/should-cargo-new-default-to-applications/3772/6 "2016-07-29T18:41:12Z")

</div>

I had another thought – should we do a better job documenting the difference between a `bin` and a `lib`? Or documenting how to switch between the two (or how to add a [lib.rs](http://lib.rs) to a binary crate, or how to add a binary to a library crate).

I’m wondering if very new users are getting hung up trying to make a choice that, in the end, is not very consequential.

---

<div class="post-metadata">

**Author:** ![djc](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/djc/32/1592_2.png) [@djc](https://internals.rust-lang.org/u/djc)\
**Post date:** [July 29, 2016, 7:08pm UTC](https://internals.rust-lang.org/t/should-cargo-new-default-to-applications/3772/7 "2016-07-29T19:08:28Z")

</div>

Going all meta: build opt-in telemetry for cargo, so we can make data-driven decisions on this stuff.

---

<div class="post-metadata">

**Author:** ![retep998](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/retep998/32/729_2.png) [@retep998](https://internals.rust-lang.org/u/retep998)\
**Post date:** [July 29, 2016, 7:29pm UTC](https://internals.rust-lang.org/t/should-cargo-new-default-to-applications/3772/8 "2016-07-29T19:29:55Z")

</div>

Yes please.

---

<div class="post-metadata">

**Author:** ![jntrnr](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jntrnr/32/3046_2.png) [@jntrnr](https://internals.rust-lang.org/u/jntrnr)\
**Post date:** [July 29, 2016, 8:47pm UTC](https://internals.rust-lang.org/t/should-cargo-new-default-to-applications/3772/9 "2016-07-29T20:47:49Z")

</div>

Telemetry is definitely interesting, but it feels like a whole conversation in itself. I’m afraid if we say “let’s wait for telemetry”, which may not come any time soon, we’re giving months/years to the current default and building up people’s muscle memory for it.

Right now cargo is not yet 1.0, and after it reaches 1.0 we shouldn’t be changing defaults (or perhaps only rarely at semver boundaries). Now is the time to make the adjustments when we can.

---

<div class="post-metadata">

**Author:** ![withoutboats](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/withoutboats/32/4560_2.png) [@withoutboats](https://internals.rust-lang.org/u/withoutboats)\
**Post date:** [July 29, 2016, 8:58pm UTC](https://internals.rust-lang.org/t/should-cargo-new-default-to-applications/3772/10 "2016-07-29T20:58:37Z")

</div>

Its actually unclear to me what cargo’s semver story is right now. For example, a change that could cause `cargo build` to start failing for any crate is likely unacceptable. That’s a whole other conversation, and I think this change is fine, but I’m just saying its not 100% certain we can make this change.

---

<div class="post-metadata">

**Author:** ![dan\_t](https://avatars.discourse-cdn.com/v4/letter/d/eada6e/32.png) [@dan\_t](https://internals.rust-lang.org/u/dan_t)\
**Post date:** [July 29, 2016, 9:03pm UTC](https://internals.rust-lang.org/t/should-cargo-new-default-to-applications/3772/11 "2016-07-29T21:03:08Z")

</div>

Why not forcing the user to make a decision?

```
cargo new bin foo
cargo new lib foo
```

---

<div class="post-metadata">

**Author:** ![jntrnr](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jntrnr/32/3046_2.png) [@jntrnr](https://internals.rust-lang.org/u/jntrnr)\
**Post date:** [July 29, 2016, 9:31pm UTC](https://internals.rust-lang.org/t/should-cargo-new-default-to-applications/3772/12 "2016-07-29T21:31:21Z")

</div>

@dan_t - I saw @withoutboats mention a similar idea. We can do that, but it has the same breakage as changing the default, as “cargo new foo” would no longer work (assuming I understand you correctly).

I personally kinda like that “cargo new something” isn’t all that much to type, so I’m kinda leaning towards keeping it around if we can.

In case anyone is curious, the [current version of the patch](https://github.com/rust-lang/cargo/pull/2921) introduces a new --lib and also prints out a status when it’s done so you can see what kind of project you created. I’m hoping that even if we don’t end up changing the default, this will help users discover the other project type(s).

---

<div class="post-metadata">

**Author:** ![jntrnr](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jntrnr/32/3046_2.png) [@jntrnr](https://internals.rust-lang.org/u/jntrnr)\
**Post date:** [July 29, 2016, 9:33pm UTC](https://internals.rust-lang.org/t/should-cargo-new-default-to-applications/3772/13 "2016-07-29T21:33:01Z")

</div>

@withoutboats - agreed that different subcommands of cargo feel change-able but others don’t. This one is kinda borderline for me since it’s unlikely to be part of someone’s build script. That said, I didn’t want to change it without first reaching out to see what other Rust folks thought.

---

<div class="post-metadata">

**Author:** ![cswords](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cswords/32/1682_2.png) [@cswords](https://internals.rust-lang.org/u/cswords)\
**Post date:** [July 29, 2016, 9:44pm UTC](https://internals.rust-lang.org/t/should-cargo-new-default-to-applications/3772/14 "2016-07-29T21:44:11Z")

</div>

> In case anyone is curious, the current version of the patch introduces a new --lib and also prints out a status when it's done so you can see what kind of project you created. I'm hoping that even if we don't end up changing the default, this will help users discover the other project type(s).

If we aren't going to change the default, it would be incredibly helpful to introduce a command to quickly swap the setup. Maybe something like `cargo add lib`/`cargo add bin` to automatically patch up a miscreated project.

---

<div class="post-metadata">

**Author:** ![withoutboats](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/withoutboats/32/4560_2.png) [@withoutboats](https://internals.rust-lang.org/u/withoutboats)\
**Post date:** [July 29, 2016, 10:00pm UTC](https://internals.rust-lang.org/t/should-cargo-new-default-to-applications/3772/15 "2016-07-29T22:00:44Z")

</div>

Looking at the PR, I am really surprised there wasn’t already a `--lib` flag! It definitely seems like it should be possible to be explicit about that.

I’m really interested in the idea of crate templates, I’ll keep thinking about that space and try to come up with a more detailed proposal.

---

<div class="post-metadata">

**Author:** ![Yohanesu75](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/yohanesu75/32/1982_2.png) [@Yohanesu75](https://internals.rust-lang.org/u/Yohanesu75)\
**Post date:** [July 29, 2016, 10:19pm UTC](https://internals.rust-lang.org/t/should-cargo-new-default-to-applications/3772/16 "2016-07-29T22:19:56Z")

</div>

Default to bin and --lib flag seems more logical to me (because I always make the mistake of creating a lib when I intend to create a bin…)

---

<div class="post-metadata">

**Author:** ![ahmedcharles](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ahmedcharles/32/4041_2.png) [@ahmedcharles](https://internals.rust-lang.org/u/ahmedcharles)\
**Post date:** [July 29, 2016, 10:34pm UTC](https://internals.rust-lang.org/t/should-cargo-new-default-to-applications/3772/17 "2016-07-29T22:34:19Z")

</div>

My perspective is that (successful) programming is about building libraries and then combining them with a small amount of ‘glue’ code to turn them into a binary. So, in that sense, lib is the right default. Though, this doesn’t suggest that there won’t be more invocations of cargo new using --bin, however. I suppose it’s a very nuanced choice and I don’t see a compelling argument for changing it.

Not everything should be reconsidered over and over, forever. At some point just going forward with what you have is best.

---

<div class="post-metadata">

**Author:** ![brson](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/brson/32/5197_2.png) [@brson](https://internals.rust-lang.org/u/brson)\
**Post date:** [July 30, 2016, 1:54am UTC](https://internals.rust-lang.org/t/should-cargo-new-default-to-applications/3772/18 "2016-07-30T01:54:12Z")

</div>

I think `--bin` makes sense as the default purely for the newbie experience of playing with Rust, even if `--lib` is more common (which I think we don’t have any data about).

---

<div class="post-metadata">

**Author:** ![anp](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/anp/32/6036_2.png) [@anp](https://internals.rust-lang.org/u/anp)\
**Post date:** [July 30, 2016, 4:17am UTC](https://internals.rust-lang.org/t/should-cargo-new-default-to-applications/3772/19 "2016-07-30T04:17:56Z")

</div>

Sounds like a good use of that GitHub on BigQuery data.

---

<div class="post-metadata">

**Author:** ![withoutboats](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/withoutboats/32/4560_2.png) [@withoutboats](https://internals.rust-lang.org/u/withoutboats)\
**Post date:** [July 30, 2016, 4:22am UTC](https://internals.rust-lang.org/t/should-cargo-new-default-to-applications/3772/20 "2016-07-30T04:22:04Z")

</div>

I agree. In the lifetime of a Rust programmer, creating a new crate is a rare occurrence, and so it is not very important to optimize for an average Rust user’s experience. In contrast, a user who has less than 30 minutes of experience with Rust is very likely to a) create a new crate, b) want to run that crate (to see ‘hello world’ or whatever else). It makes a lot of sense to optimize for the early user’s experience here, since having to fix their mistake of not passing `--bin` would be a frustrating and maybe confusing distraction.

[Next page](https://internals.rust-lang.org/t/should-cargo-new-default-to-applications/3772.md?page=2)
