Originally published on php2rust.dev — part of the PHP to Rust course. View this lesson on the original site.

To create a library (reusable crate) instead of a binary, use the --lib flag:

cargo new --lib your_library_name

Or, in an existing directory:

cargo init --lib

This generates src/lib.rs instead of src/main.rs. Libraries contain reusable functions and modules but no main function.

  • Binary, src/main.rs, Standalone executable, run with cargo run
  • Library, src/lib.rs, Reusable code, imported by other projects

What's next

Next: Step 1: Setting up your project, build a simple CLI calculator to practice these concepts.

From PHP to Rust — Getting Started with Rust