Skip to content

It is not possible to specify a default binary #2200

Closed
@JanLikar

Description

@JanLikar
Contributor

I think it would be really great if one would be able to specify a default binary in Cargo.toml, so cargo run could be used without --bin argument when there are more binaries in a crate.

Activity

steveklabnik

steveklabnik commented on Dec 7, 2015

@steveklabnik
Member

👍 , with the default default being set to src/main.rs

JanLikar

JanLikar commented on Dec 18, 2015

@JanLikar
ContributorAuthor

Do you think something like default-bin in [package] would be ok?

alexcrichton

alexcrichton commented on Dec 18, 2015

@alexcrichton
Member

I'd be a little hesitant to move on this just yet. For example there are other questions I'd have such as:

  • Should there also be a default test binary? Default bench binary?
  • Does this interact with commands like cargo rustc and cargo rustdoc where the default takes precedence over other targets?
  • Does this interact with cargo install where the default is the only one installed unless otherwise specified?

I'd want to flesh this out a bit more and see how it fits into the whole Cargo picture before landing it. FOr now --bin foo doesn't seem too costly at least for something that's largely only used during development.

JanLikar

JanLikar commented on Dec 18, 2015

@JanLikar
ContributorAuthor

I agree, I will think this through.

jadencarver

jadencarver commented on Mar 5, 2016

@jadencarver

+1

andreastt

andreastt commented on Nov 26, 2016

@andreastt
Contributor

I think having a run or default-run option, akin to build, under [package] would be a good idea for the reason that one might implement a program with “subcommands” as multiple binaries with the default program as the sub-program dispatcher.

Using git(1) as an example:

  • /usr/bin/git
  • /usr/lib/git-core/git-commit
  • /usr/lib/git-core/git-diff

When using git commit in the shell this passes "commit" as a string to /usr/bin/git, which ends up calling /usr/lib/git-core/git-commit with more flags.

Hypothetically imaging implementing this using cargo, calling cargo run -- commit is more concise than cargo run --bin git-commit -- -c some=flag.

zimond

zimond commented on Dec 5, 2016

@zimond

If developing using VSCode, the RustyCode plugin requires only one binary to be specified. So I think this feature could be useful.

huytd

huytd commented on Feb 19, 2017

@huytd

What is the status of this issue right now? It is very helpful if you have multiple binaries in a project!

RalfJung

RalfJung commented on Jun 8, 2017

@RalfJung
Member

Should there also be a default test binary? Default bench binary?

I feel like test binaries are a very separate thing anyway, and cargo test per default runs all of them (right?). I never used bench, but it seems similar to test?

Does this interact with commands like cargo rustc and cargo rustdoc where the default takes precedence over other targets?
Does this interact with cargo install where the default is the only one installed unless otherwise specified?

I think this should affect cargo run only. It could be called "default-run" or so, like @andreastt suggested?

sunjay

sunjay commented on Jul 25, 2017

@sunjay
Member

What about something in the [[bin]] configuration like the following?

[[bin]]
name = "my-default-executable"
default = true

That fits into the existing way to configure binaries and will support future additions as well since it isn't as "one-off" as "default-run". :)

mcobzarenco

mcobzarenco commented on Nov 15, 2017

@mcobzarenco

@sunjay I reckon @JanLikar's suggestion:

Do you think something like default-bin in [package] would be ok?

is preferable as it disallows in its representation having more than one default.

GildedHonour

GildedHonour commented on Jan 28, 2018

@GildedHonour

what's the status of this, is it already possible?

added a commit that references this issue on Jul 16, 2018

Auto merge of #5710 - RalfJung:default-run, r=alexcrichton

e325bff
dhbradshaw

dhbradshaw commented on Jun 10, 2019

@dhbradshaw
vladikoff

vladikoff commented on Apr 17, 2020

@vladikoff

TL;DR it works this way:

[package]
name = "namehere"
...
default-run = "binaryname"

[[bin]]
name = "binaryname"
path = "src/main.rs"

[[bin]]
name = "somethingelse"
path = "src/somethingelse.rs"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-featuresArea: features — conditional compilationCommand-run

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @steveklabnik@alexcrichton@vladikoff@jadencarver@dhbradshaw

      Issue actions

        It is not possible to specify a default binary · Issue #2200 · rust-lang/cargo