I’m new to Rust and trying to understand the practical differences between:
cargo new my_project
cargo new --lib my_project
I’m new to Rust and trying to understand the practical differences between:
cargo new my_project
cargo new --lib my_project
The only significant difference is whether it creates a src/lib.rs
file (that will be compiled into a library crate which you can depend on from another package) or a src/main.rs
file (that will be compiled into a binary crate which you can run). You can always change it later if you want to restructure your project.
Never. Use cargo init
or cargo init --lib
instead
It does all the same things except it also works without a path argument to initialize a new package in the current working directory.
I think it's worth clarifying: cargo new foo
creates a new directory named foo
with a package of the same name, while cargo init
makes a new package in the current directory, naming the package after the directory.
... and cargo init foo
does the same as cargo new foo
, which is why I was (jokingly) saying to never use the latter. The init command can do everything that the new command does.
BTW, the change is trivial. It's really just a matter whether src/lib.rs
or src/main.rs
exists. There's no other configuration to change. There's no special boilerplate needed.
Cargo.toml
supports more customization of library paths and binaries in the project, but in the default you get from cargo new
, it's all automatic.