Creating a library and executable in one project

How to create a library and an executable file in one project? I just want to test my library while working on it, and using tests is not always the best way to do this. I believe that I need to use [lib] and [???] , but I did not find information about this on crates.io.

+5
source share
1 answer

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

To add both a library and an executable file to your mailbox (BTW, a mailbox can contain only one library), you need to define them in the [lib] and [[bin]] sections:

 [lib] name = "yourcrate" [[bin]] name = "yourcrate_bin_1" [[bin]] name = "yourcrate_bin_2" 

With the above default, Cargo will look for the library root directory in src/lib.rs and for binaries in src/bin/yourcrate_bin_1.rs and src/bin/yourcrate_bin_2.rs . You can change the paths to the root files of the box using the path option:

 [[bin]] name = "yourcrate_bin_2" path = "src/yourcrate_bin_2.rs" 
+4
source

All Articles