#Packaging a CLI in a Library Crate

3 messages · Page 1 of 1 (latest)

dusky iron
#

Hey! I have a library type crate. I want to allow other crates to include and use this library, but I also want to package in an executable CLI crate for debugging. Is there a way I can make an executable type "sub crate" for the library so I can 1. keep everything in one project (CLI and the library) 2. still use the library component in other executables?

open patio
# dusky iron Hey! I have a library type crate. I want to allow other crates to include and us...

You can have a binary target and a library target in the same crate.
You can even have multiple binary targets (but only up to one library target).
https://doc.rust-lang.org/cargo/reference/cargo-targets.html

Cargo can normally auto-detect binaries and libraries if you use the file layout it expects, e.g. ```
├── src/
│ ├── lib.rs
│ ├── main.rs
│ └── bin/
│ ├── named-executable.rs
│ ├── another-executable.rs
│ └── multi-file-executable/
│ ├── main.rs
│ └── some_module.rs

The default library file is src/lib.rs.
The default executable file is src/main.rs.
Other executables can be placed in src/bin/.
(source) https://doc.rust-lang.org/cargo/guide/project-layout.html
dusky iron
#

yep! this is what i was looking for!