Rust programming language – Getting Started

Rust ProgrammingServer Side ProgrammingProgramming

The first part of getting hands-on experience with Rust is installing Rust. In order to install Rust, we need a Rust installer.

Rustup is a version management tool and also an installer that helps in installing Rust in your local machine.

If you’re running Linux, macOS, or another Unix-like OS, then we simply need to run the following command in the terminal −

curl --proto ‘=https’ --tlsv1.2 -sSf https://sh.rustup.rs | sh

The above command will install Rust on your local machine.

If you are on Windows, then you can download the .exe file from this link rustup-init.exe

Keeping Rust Updated

Though Rust is updated frequently, but you can always make sure that it is up-to-date by running the following command −

rustup update

Cargo: the package manager

  • Once you’ve installed Rustup, you’ll also get the latest version of Rust’s package manager and build tool which is also known as Cargo. Cargo is essential for Rust, and it allows us to perform the following operations −
  • build your project with − cargo build
  • run your project with − cargo run
  • Test your project with − cargo test
  • Build documentation for your project with − cargo doc

We can test whether we’ve correctly installed Rust and Cargo by running the following command in any terminal of our choice −

cargo --version

Creating a new project

Let’s make use of Cargo to create a new Rust project. In your terminal, run the following command −

cargo new hello-tutorialspoint

The above command will generate a new directory called hello-tutorialspoint with the following files −

hello-tutorialspoint
|-- Cargo.toml
|-- src
   |-- main.rs

Here,

  • Cargo.toml − the manifest file for Rust.
  • src/main.rs − where your application code will be written.

It’s always a tradition that we write a “Hello, world!” program whenever we are learning a new programming language. In Rust, Cargo provides us with the same, we just need to run the following command in our terminal −

cargo new

The above command will generate a “Hello, world!” project. In order to run the project, we need to move into the new directory that we made and run the following command in the terminal −

cargo run

After running this command, you’ll have something similar to this −

Compiling hello-tutorialspoint v0.1.0 (/Users/immukul/hello-tutorialspoint)
   Finished dev [unoptimized + debuginfo] target(s) in 2.39s
   Running `target/debug/hello-tutorialspoint`
Hello, world!
raja
Published on 20-Feb-2021 06:50:43
Advertisements