RUST: Quick Start & Exploring Cargo

Table of contents
Reading Time: 3 minutes

This blog will guide you through the brief introduction to RUST, installation, uninstallation, version updating and quick start with its own build tool CARGO.

RUST – safe, concurrent, and practical language, much similar to C++ (syntactically) supports functional as well as imperative-procedural paradigms.

Setting up RUST in your system:

  1. For Linux or Mac:

It’s quick and easy to install and setup your system for RUST

Open the terminal and execute:

curl https://sh.rustup.rs -sSf | sh

This will install the current stable version without worrying about downloading the archive file, extracting it and configuring the environment variables.

After completion of installation, it will show Rust is installed now. Great!

To confirm the installation and check the version by executing:

$ rustc –version

Note: Generally linker is pre-installed with OS, in case you get an exception stating that linker could not be executed, it might be because linker is missing. You can fix this issue easily, just install a C compiler, even though RUST is based on C/C++ it requires the compiler too.

  1. For Windows:

Simply open this link and follow the instructions.

Note: you should have “C++ build tools for Visual Studio 2013 or later” installed in the system. You can install it from here.

Uninstalling the RUST:

Execute the following script on the shell:

$ rustup self uninstall

Updating the RUST to the latest version:

Execute the following script on the shell:

$ rustup update

# Now you might be wondering –  what is RUSTUP?
It’s “The Rust toolchain installer” used to manage the language installation.

RUSTUP also include a local doc to your system at the time of installation, you can access it by executing the following script on the shell:

$ rustup doc

Let’s write a simple Hello, world! program using RUST:

Create a file main.rs and copy paste the following code in it:

save the file and open the terminal with current directory, execute following command to compile and generate an executable file:

$ rustc main.rs

It will create a main executable file, you can run it in terminal it will show the output as

$ ./main
Hello, world!

While working with single file it’s quite easy and fast to compile and execute the file, but while working with a large project having a lot of files it will be difficult to manage the directories, files, and compilation, for that RUST provide its own build tool  CARGO to manage and build the project.

You must be wondering or looking for the script to skip all the stuff and directly install CARGO but wait you don’t need to run any script or download any archive to install CARGO.

Just check by executing: $ cargo –version it’s there, so don’t overheat up your brain, CARGO is pre-installed with the Rust setup.

So, without wasting time let’s skip to creating a new project through CARGO:

$ cargo new hello_world –bin

new: this is used to tell cargo that we are creating a new project
hello_world: new is followed by project name, which is also used to create the directory
— bin: this states that it is a binary (executable) project, there are two type of projects binary and library.

let’s explore hello_world directory in detail:

  • src/
    This path contains all the source code file, you can check there is already one main.rs file, binary project execute with the main file containing the main method, something like this:
  • Cargo.toml
    It is a rust project configuration file .toml (Tom’s Obvious, Minimal Language), it contains details about the project name, version and its dependencies (external crates). We will talk in more detail in our next blog with the example.

Now, let’s build and run this project:

$ cargo build

This will compile your src/ code and create an executable file hello_world in target/debug/ directory (first time it will also create the target/ directory)

This is same as compiling the main.rs file directly through the rustc compiler and generating executable file:

$ rustc main.rs

To skip these two-step process, we can also do these in a one go by executing:

$ cargo run
Hello, world!

Other command-line option (most useful) in Cargo:

  1. Check – much faster than build, used to compile the src code, it will not generate the executable file.
  2. Clean – delete the target directory
  3. Test – run the test case of the project
  4. Update –  update version of project dependencies
  5. Doc – generate project documentation

Note: you can check all the cargo option by executing: cargo –list

ReferenceGetting Started – The Rust Programming Language

Written by 

Sourabh Verma is a Software Consultant with experience of more than 2 years. He has a deep understanding of Java and familiar with Spring Framework, JPA, Hibernate, JavaScript, Spark, Scala, AngularJS, Angular 4. He is an amazing team player with self-learning skills and a self-motivated professional. He loves to play with Real-time problems, Big data, Cloud computing, Agile Methodology and Open Source Technology. He is eager to learn new technologies and loves to write blogs and explore nature.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading