Rust programming language – Getting Started



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!
Updated on: 2021-02-20T06:53:00+05:30

351 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements