7

How do I create a library and executable in one project? I just want to test my library while working on it and using tests isn't always the best way to do that. I believe I have to use [lib] and [???] but I haven't found the information about that at crates.io.

1 Answers1

3

Indeed, it's strange that crates.io does not have a clear example of this.

To add both a library and an executable to your crate (BTW, the crate can only have one library in it), you need to defined them in [lib] and [[bin]] sections:

[lib]
name = "yourcrate"

[[bin]]
name = "yourcrate_bin_1"

[[bin]]
name = "yourcrate_bin_2"

With the above by default Cargo will look for the library crate root in src/lib.rs and for binaries in src/bin/yourcrate_bin_1.rs and src/bin/yourcrate_bin_2.rs. You can change paths to the crate root files with path option:

[[bin]]
name = "yourcrate_bin_2"
path = "src/yourcrate_bin_2.rs"
Vladimir Matveev
  • 120,085
  • 34
  • 287
  • 296
  • what does bin has the double "[["? –  Aug 29 '15 at 13:36
  • 2
    That's because multiple `bin` sections are possible. See [TOML](https://github.com/toml-lang/toml) description for more information, in particular, [this](https://github.com/toml-lang/toml#array-of-tables) section. – Vladimir Matveev Aug 29 '15 at 14:04