DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Languages
  4. Rust: Quick Start and Exploring Cargo

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.

Sourabh Verma user avatar by
Sourabh Verma
·
Sep. 04, 18 · Tutorial
Like (3)
Save
Tweet
Share
6.24K Views

Join the DZone community and get the full member experience.

Join For Free

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

Rust (programming language)

Published at DZone with permission of Sourabh Verma, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • The Real Democratization of AI, and Why It Has to Be Closely Monitored
  • The 31 Flavors of Data Lineage and Why Vanilla Doesn’t Cut It
  • Silver Bullet or False Panacea? 3 Questions for Data Contracts
  • Connecting Your Devs' Work to the Business

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: