How to Install Rust and Create Your First Application

Introduction

Rust is a programming language that combines speed, safety, and simplicity, making it a great choice for modern software development. Whether you’re an experienced developer or just getting started, this guide will show you how to install Rust and create your first application in just a few steps. By the end, you’ll have Rust fully set up and a working program ready to run.

Versions Used

  • Rust 1.84.0
  • Cargo 1.84.0 (Rust’s package manager, included with the installation)

1. Installing Rust

Rust is installed via rustup, a tool that makes it easy to manage Rust versions. To install it:

On Windows:

Open a PowerShell terminal as administrator and run:

winget install --id Rustlang.Rustup -e

On macOS:

Open the terminal and install rustup with:

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

Follow the on-screen instructions to complete the installation.

On Linux:

Run the following command in the terminal:

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

After installation, update the shell configuration with:

source $HOME/.cargo/env

2. Verify the Installation

To check if Rust was installed correctly, run:

rustc --version

If the Rust version appears on the screen, the installation was successful.

3. Creating a New Rust Application

To start developing with Rust, you need to create a new project using cargo, Rust’s official tool for managing projects and dependencies.

Open a terminal and follow these steps:

  1. Create a new Rust project with the command:
   cargo new hello_rust

This command will generate a hello_rust folder containing the basic structure of a Rust project.

  1. Navigate to the project directory:
   cd hello_rust

4. Writing the First Program

After creating the project, it’s time to write your first Rust program.

  1. Open the project with Visual Studio Code (or your preferred editor):
   code .
  1. Open the src/main.rs file and modify its content by inserting the following code, which prompts the user for input and processes it:
   use std::io;

   fn main() {
       println!("Enter your name:");

       let mut name = String::new();
       io::stdin()
           .read_line(&mut name)
           .expect("Error reading input");

       let name = name.trim();
       greet(name);
   }

   fn greet(name: &str) {
       println!("Hello, {}! Welcome to the world of Rust!", name);
   }

Save the file, and you’re ready to run your first Rust program!

5. Running the Program

Now that the code is ready, you can compile and run your first Rust program.

  1. Open a terminal in the project directory and compile the code with:
   cargo build

This command will create an executable file inside the target/debug/ folder.

  1. To run the program directly, use:
   cargo run

After running the command, the terminal will prompt you to enter a name. Type your name and press Enter. You should see a personalized welcome message, for example:

   Enter your name:
   Alice
   Hello, Alice! Welcome to the world of Rust!
  1. If you want the program to automatically recompile and rerun every time you make a change, you can install and use cargo watch:
   cargo install cargo-watch

Then, to run the program in automatic mode:

   cargo watch -x run

This command will monitor the project files and rerun the program whenever a change is detected.

Conclusion

Now you have installed Rust and created your first application. From here, you can explore more advanced Rust concepts, such as memory management, threading, and macros.

Additional Resources

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top