Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Make a Rust Project Both a Library and a Binary

Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!

.
├── Cargo.toml
└── src
    ├── main.rs
    │   
    └── lib.rs
[package]
name = "package_name"
version = "0.0.1"
authors = ["me <me@gmail.com>"]

[lib]
name = "lib_name"
path = "src/lib.rs"

[[bin]]
name = "binary_name"
path = "src/bin.rs"
  1. It seems that you have to use relative import when using the above structure ...

  2. lib.rs is the entrance place of the library (similar to that main.rs is the entrance place of the binary). It is not a module!

References

Rust package with both a library and a binary?

Cargo, Crates and Basic Project Structure