How to create a package in Cargo that contains both a library and an executable?
Hi everyone! I just started learning Rust and am trying to organize my project. I have some logic I want to reuse: for example, functions for working with numbers (factorial, Fibonacci numbers). I want to make these functions available as a library (so they can be imported in other projects via `use`), but also have an executable (binary) that runs and demonstrates these functions through the console.
I created a project with `cargo new my_project`, and it only created `src/main.rs`. I read that you can add `src/lib.rs`, but how do I properly configure `Cargo.toml`? Do I need to write something like `[lib]` or `[[bin]]`? And how do I run the binary when I want to use it, while making the library available for other crates to import?
No answers yet.
Log in to leave an answer
Blogs