Category: Rust

First Rust project using cargo package manager

If this is the first time you are using Rust you will have to install Rust on your Windows machine.

To install see this link – https://www.rust-lang.org/tools/install

Check the version of Rust-

rustup --version

Check the version of Rsut compiler

rustc --version

Create a Rust project using Cargo

Cargo is a build and package manager.

For creating a new Rust project using Cargo package manager-

cargo new first_rust_prj

This should create follwoing folder structure with main.rs and Cargo.toml (configuration) file.

You can create a project without using Cargo. Create a src directory and create appropriate Cargo.toml namually. To create toml file use init command

cardo init

Building and Running Cargo project

Build the Cargo project using following command-

cargo build

To compile and run directly from the project folder, use following command-

cargo run

Error compiling the project-

error: linker link.exe not found

So the pre-requisite for windows machine is to have MS C++ build tools. Install the smae from here. It should be around 5 GB.

https://visualstudio.microsoft.com/visual-cpp-build-tools

Now this should run the project –

To check the code without producing an executable use follwoing command-

cargo check

Building for Release

Use following command when project is ready for release-

cargo build --release

References-

https://doc.rust-lang.org/book/ch01-01-installation.html#installing-rustup-on-linux-or-macos

Loading

All about Rust Cargo commands

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.

Loading