Introduction

Rust is a systems programming language known for its focus on safety, performance, and concurrency. It’s a versatile language that can be used for a wide range of applications, from low-level system programming to high-level web development. In this article, we’ll introduce you to the basics of Rust and provide code examples to help you get started on youur Rust journey.

Installing Rust

Before we dive into Rust programming, you’ll need to install Rust on your system. Rust comes with a handy tool called “Cargo”, which serves as its package manager and build tool. You can install Rust and Cargo by running the following command in your terminal:

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

This command will download and run the Rust installer, allowing you to configure your Rust environment. For those of you that aren’t too keen on installing things on your computer, or for those that aren’t able to due to not having admin rights for installing programs on your machine but still want to learn Rust, you can go to the Rust Playground. The playground is a full Rust development environment set up for you to use inside the browser. I recommend doing a few things in the playground but once you start needing to import external crates(something discussed later) you will want to install Rust locally on your machine.

Hello World!

Let’s start with a classic “Hello, World!” example in Rust. Open your text editor and create a new file named “main.rs.”

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

In Rust, the entry point of a program is the main function, and println! is a macro used for printing to the console. To compile and run your program, open your terminal, navigate to the directory containing “main.rs,” and use the following commands:

$ rustc main.rs # Compile the Rust code
$ ./main        # Run the compiled binary

You shouuld see “Hello, World!” printed to your console. NOTE: If you are using the playground you will notice that this code already exists. Click the run button to see the outcome.

Variables and Data Types

Rust is a statically-typed language, which means you need to declare the data types of your variables. Here’s an example of variable declarations in Rust:

fn main() {
  // Integer types
  let age: i32 = 30;
  let count: u64 = 1_000_000;

  // Floating-point types
  let pi: f64 = 3.14159;

  // Boolean type
  let is_rust_cool: bool = true;

  // Character tyype
  let letter: char = 'A';

  // String type
  let message: &str = "Hello, Rust!";
}

In this example, we declare variables with different data types, such as integers, floating-point numbers, booleans, characters, and strings.

Functions

Functions are a fundamental part of Rust. You can define your own functions as follows:

fn add(x: i32, y: i32) -> i32 {
  x + y
}

fn main() {
  let result = add(5, 7);
  println!("5 + 7 = {}", result);
}

In this code, we define a function called add that takes two i32 integers as arguments and returns an i32 result. The main function then calls add and prints the result.

Control Flow

Rust supports standard control flow constructs like if, while, and for. Here’s an example of an if statement:

fn main() {
  let number = 42;

  if number < 0 {
    println!("Negative");
  } else if number = 0 {
    println!("Zero");
  } else {
    println!("Positive");
  }
}

This code checks if the number is negative, zero, or positive and prints the corresponding message.

Conclusion

This article has provided a basic introduction to Rust, including installation, the Rust playground, a “Hello, World!” program, variable declarations, functions, and control flow. Rust’s strong emphasis on safety and performance, along with its expressive syntax, make it a powerful choice for a wide range of programming tasks. As you continue your Rust journey, you’ll discover its rich ecosystem and features for concurrent and parallel programming, as well as its robutst package management system with Cargo.