When you start working in Rust, you'll quickly learn about Cargo and love it!

It's a package manager and build system that helps you manage your Rust projects.

Coming from Python, I found it very similar to Poetry, which is a great tool as well).

Features

  • Dependency management: Cargo.toml
  • Virtual environments: Cargo.toml and Cargo.lock
  • Lock files: Cargo.lock
  • Build system: cargo build, cargo run, cargo test, cargo doc
  • Publishing to crates.io: cargo publish

Cargo vs Poetry Commands Cheat Sheet

Here's a quick comparison of some common commands in Cargo and Poetry. Notice how similar they are! 😎

ActionCargo CommandPoetry Command
Initialize a projectcargo new project_namepoetry new project_name
Initialize in current dircargo initpoetry init
Add a dependencycargo add dependency_namepoetry add dependency_name
Remove a dependencycargo remove dependency_namepoetry remove dependency_name
Update dependenciescargo updatepoetry update
List dependenciescargo treepoetry show --tree
Build the projectcargo buildpoetry build
Run the projectcargo runpoetry run python your_script.py
Run testscargo testpoetry run pytest (requires pytest)
Generate documentationcargo docNot built-in, use a tool liked Sphinx
Build release binarycargo build --releasepoetry build (for packaging)
Run release binarycargo run --releaseNot directly supported
Format codecargo fmtpoetry run black . (requires black)
Lint codecargo clippypoetry run flake8 . (requires flake8)

One cool thing I noticed is that generating docs and testing is built-in in Cargo, while in Python you need to use external tools like Sphinx and pytest.

Conclusion

If you love Poetry, you'll love Cargo too! They are both great tools for managing your projects and dependencies.

Funny enough, in Python I actually stopped using poetry in favor of venv and pip because I felt this was faster and all I needed. But in Rust so far I feel Cargo is more integrated and I am using it more (well, don't have much choice, have I? 😅).

I think it's mainly because I don't have to deal with virtual environments in Rust, which is a big plus. Also it infers the binary name from the project name, which is nice. 📈

This post was just to quickly compare Cargo and Poetry and show you how similar they are. I hope you found it helpful! 😊

I am sure I will do more in-depth posts on Cargo in the future. Stay tuned! 🚀