Rust is a systems programming language that emphasizes performance and safety, particularly safe concurrency. This guide will provide an overview of Rust's syntax, features, and ecosystem.

Installation

To get started with Rust, you'll need to install the Rust toolchain. You can download and install it from the official website.

Hello World

A simple "Hello World" program in Rust looks like this:

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

Variables and Data Types

In Rust, variables are immutable by default. To make them mutable, you must explicitly declare them as such using the mut keyword.

let mut x = 5;
x = 6;

Rust provides a variety of data types, including:

  • Integer types: i32, i64, i128, u32, u64, u128, etc.
  • Floating-point types: f32, f64
  • Boolean: bool
  • Char: Single Unicode scalar value
  • String: UTF-8 encoded text

Structs and Enums

Structs are used to group related data together, while enums are used to define a type with a set of possible variants.

struct Person {
    name: String,
    age: u32,
}

enum Mood {
    Happy,
    Sad,
    Angry,
}

Functions

Functions in Rust are defined using the fn keyword. Here's an example:

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

Modules and Crates

Rust uses modules and crates to organize code. Modules are used to group related items together, while crates are the smallest unit of Rust software.

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

To use the math module, you need to add it to the top of your file:

use math;

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

Concurrency

Rust provides powerful tools for writing concurrent code. One of the most popular is the async/await syntax.

async fn hello() {
    println!("Hello!");
}

#[tokio::main]
async fn main() {
    hello().await;
}

Resources

For more information on Rust, check out the following resources:

[

Rust_Logo
]

[

Rust_By_Example
]

[

Rust_Community
]