8

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.

0

1 Answer 1

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"
Sign up to request clarification or add additional context in comments.

2 Comments

what does bin has the double "[["?
That's because multiple bin sections are possible. See TOML description for more information, in particular, this section.