Last Updated on January 3, 2025 by sandeeppote
Cargo is Rust’s build system and package manager. Most developers use this tool to manage their Rust projects because Cargo handles a lot of tasks for you, such as building your code, downloading the libraries your code depends on, and building those libraries.
Cargo commands and its usage-
Check the cargo version
cargo --version
Create a new project
Create a new project using Cargo.
cargo new <<project_name>>
Build the Cargo project
Build command compiles the project and create’s an executable file in /debug/ folder. Running the cargo build command updated the Cargo.lock file which keeps the trakc of exact version of dependecies in the project.
cargo build
Running a Cargo project
Cargo run compiles the project and runs the resultant executable all in one command. So instead of remembering the cargo build and path to execute the excutable, Cargo run does both for you which is more conveninent
cargo run
Check your code
If you want to quickly check if the code to make sure it complies susccesfully without using build or run command, you can use check command. This nmusch faster than build because it skips the step to create an executable.
cargo check
Update the project dependencies
Update the dependeincies of you project using update command. This ignores the Cargo.lock file which has all the dependencies. update command will look for one higher version than current version e..g: if the current version is 0.7.5 and the latest version is 0.8.0. But if there is next higher version i.e. 0.7.6, then update command will udpate the dependency to 0.7.6. Also after update the Cargo.lock file is udpated.
If you want to jump to a specific version, as per above case to 0.8.0. Update hte Cargo.toml file dependencies section.
cargo update
This is WIP, there is lot more to add here.