Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Rust: Quick Start and Exploring Cargo

DZone's Guide to

Rust: Quick Start and Exploring Cargo

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

· Web Dev Zone ·
Free Resource

Building real-time chat? Enroll in a Free Course on Mobile Chat Development. 

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

Rust is a safe, concurrent, and practical language, = similar to C++ (syntactically) that supports functional as well as imperative-procedural paradigms.

Setting Up Rust on Your System

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 completing installation, it will show Rust is installed. Great!

To confirm the installation and check the version by executing:

$ rustc -version

Note: Generally, linker is pre-installed with your 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.

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 Rust:

Execute the following script on the shell:

$ rustup self uninstall

Updating Rust to the Latest Version

Execute the following script in 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 includes 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:

fn main() {
    println!("Hello, world!");
}

Save the file and open the terminal with the current directory and execute the 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 that has 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 scripts or download any archives to install CARGO.

Just check if it's there by executing: $ cargo -version. 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

To breakdown the above code:

  • new: this is used to tell cargo that we are creating a new project.

  • hello_world: new is followed by the 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 the hello_world directory in detail:

  • This path contains all the source code files. You can check that there is already one main.rs file. The binary project executes with the main file containing the main method, something like this:
    fn main() {
        println!("Hello, world!");
    }
  • Cargo.toml - TOML is a Rust project configuration file (Tom's Obvious, Minimal Language), that contains details about the project name, version, and its dependencies (external crates). We will talk in more detail abou tthis in our next post.

Now, let's build and run this project:

$ cargo build

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

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

$ rustc main.rs

To skip this 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 

Reference: Getting Started - The Rust Programming Language

Power realtime chat, IoT and messaging apps at scale. Pubsub realtime messaging, functions, chat, presence, push, notifications, blocks catalog and more.

Topics:
web dev ,rust ,tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}