Getting Started

Introduction

Lets walk through how to setup the Rust development environment and write our first Rust program.

Installing Rust

To start coding in Rust, you need to install the Rust programming language and its associated tools.

Step 1: Install Rust

  1. Open your terminal.

  2. Run the following command to install Rust using rustup, the Rust toolchain installer:

    curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
    
  3. Follow the on-screen instructions to complete the installation.

Step 2: Configure Your Environment

After installing Rust, you need to configure your development environment.

  1. Add the Rust bin directory to your system's PATH:

    source $HOME/.cargo/env
    
  2. Verify the installation by checking the Rust version:

    rustc --version
    

Writing Your First Rust Program

Now that we have Rust installed, let's write our first Rust program.

Step 1: Create a New Project

  1. Open your terminal.

  2. Create a new Rust project using cargo, Rust's package manager and build system:

    cargo new hello_rust
    
  3. Navigate to the project directory:

    cd hello_rust
    

Step 2: Write the Code

  1. Open the main.rs file located in the src directory with your preferred code editor.
  2. Replace the content with the following code:
fn main() {
   println!("Hello, Rust!");
}

Step 3: Run the Program

  1. Compile and run your Rust program using cargo:

    cargo run
    
  2. You should see the following output:

    Hello, Rust!
    

Basic Syntax and Features of Rust

Let's briefly go over some basic syntax and features of Rust.

Variables

Rust variables are immutable by default. Use let to declare variables.

#![allow(unused)]
fn main() {
let x = 5;

println!("The value of x is: {}", x);
}

To make a variable mutable, use mut.

#![allow(unused)]
fn main() {
let mut y = 5;

y = 6;

println!("The value of y is: {}", y);
}

Functions

Functions are declared using the fn keyword.

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

fn add(a: i32, b: i32) -> i32 {
    a + b
}

Control Flow

Rust supports typical control flow constructs such as if, else, and loop.

#![allow(unused)]
fn main() {
let number = 6;

if number % 2 == 0 {
    println!("The number is even.");
} else {
    println!("The number is odd.");
}

let mut counter = 0;
let result = loop {
    counter += 1;
    if counter == 10 {
        break counter * 2;
    }
};
println!("The result is: {}", result);
}

Comments

Rust supports single-line and multi-line comments.

#![allow(unused)]
fn main() {
// This is a single-line comment

/* This is a
multi-line comment
*/
}

Data Types

Rust supports several data types, including integers, floating-point numbers, booleans, strings, arrays, and tuples.

#![allow(unused)]
fn main() {
// i32 is an integer type
let x: i32 = 5;

// &str is a string slice
let a: &str = "Hello, Rust!";

// [i32; 3] is an array of 3 integers
let b: [i32; 3] = [1, 2, 3];

// (i32, &str) is a tuple of an integer and a string
let c: (i32, &str) = (1, "hello");

// bool is a boolean type
let d: bool = true;
}

Generics

Rust supports generic programming with generics. Generics are used to write reusable code with different types.

#![allow(unused)]
fn main() {
fn add<T: std::ops::Add<Output = T>>(a: T, b: T) -> T {
    a + b
}

let result = add(1, 2);
println!("Result: {:?}", result);

let result = add(1.3, 2.3);
println!("Result: {:?}", result);
}

This code will work with any type that implements the std::ops::Add trait. For example, i32 and f32 can both be passed as arguments to the add function and their sum will be returned.

Conclusion

In this episode, we've set up the Rust development environment, explored some basic syntax and features of Rust, and wrote our first Rust program. In the next episode, we will dive into understanding Big O Notation and analyzing time complexity.

Thank you for joining me in this journey to learn Rust and computer science concepts. Stay tuned for the next episode!