Rust Fundamentals

by mahidhar

Let's dive into each section with elaborated explanations and examples using data related to the most visited cities or places.

1. Fundamentals:

Introduction to Rust:

Rust is a systems programming language known for its safety, concurrency, and performance. It was developed by Mozilla Research and is designed to be memory-safe without sacrificing performance. Rust achieves this through its ownership system, which ensures memory safety at compile time. Additionally, Rust provides powerful concurrency primitives, making it suitable for building high-performance, concurrent applications.

Simple Example:

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

Complex Example:

code
struct City {
    name: String,
    population: u32,
    attractions: Vec<String>,
}

impl City {
    fn new(name: &str, population: u32, attractions: Vec<String>) -> City {
        City {
            name: name.to_string(),
            population,
            attractions,
        }
    }

    fn display_attractions(&self) {
        println!("Attractions in {}: {:?}", self.name, self.attractions);
    }
}

fn main() {
    let attractions = vec![
        "Eiffel Tower".to_string(),
        "Louvre Museum".to_string(),
        "Notre-Dame Cathedral".to_string(),
    ];
    let paris = City::new("Paris", 2141000, attractions);
    paris.display_attractions();
}
Installation and Setup:

To install Rust, visit the official Rust website and follow the instructions for your operating system. Rust's toolchain includes the Rust compiler (rustc), package manager (Cargo), and other necessary tools for Rust development. Once installed, you can set up your development environment using your preferred text editor or IDE with Rust support.

2. Language Basics:

Syntax and Variables:

Rust's syntax is similar to C and C++, with some modern features borrowed from functional programming languages. Variables in Rust are immutable by default, meaning once a value is assigned to a variable, it cannot be changed. However, you can use the mut keyword to declare mutable variables.

Simple Example:

code
fn main() {
    let x = 5; // immutable variable
    let mut y = 10; // mutable variable
    y += 1;
    println!("x: {}, y: {}", x, y);
}

Complex Example:

code
fn main() {
    let city_name = "Tokyo"; // immutable string
    let mut population = 13929286; // mutable integer
    population += 10000; // increase population
    println!("{} has a population of {}", city_name, population);
}
Data Types:

Rust has several primitive data types, including integers, floating-point numbers, booleans, characters, and strings. It also has compound data types such as tuples, arrays, structs, and enums.

Simple Example:

code
fn main() {
    let number: u32 = 42; // unsigned 32-bit integer
    let is_raining: bool = true; // boolean
    let letter: char = 'A'; // character
    let message: &str = "Hello, Rust!"; // string slice
}

Complex Example:

code
struct Place {
    name: String,
    coordinates: (f64, f64),
}

enum Landmark {
    Monument(String),
    NaturalWonder(String),
}

fn main() {
    let london = Place {
        name: "London".to_string(),
        coordinates: (51.5074, -0.1278),
    };

    let landmark = Landmark::Monument("Big Ben".to_string());

    match landmark {
        Landmark::Monument(name) => println!("The {} is in {}", name, london.name),
        _ => (),
    }
}
Control Flow:

Control flow in Rust includes if expressions, loops, and pattern matching. Rust's match expression is particularly powerful and is used extensively for pattern matching.

Simple Example:

code
fn main() {
    let number = 5;

    if number < 10 {
        println!("Number is less than 10");
    } else {
        println!("Number is greater than or equal to 10");
    }
}

Complex Example:

code
fn main() {
    let city = "Paris";

    match city {
        "London" => println!("London is in the UK"),
        "Paris" => println!("Paris is in France"),
        _ => println!("City not found"),
    }
}

This covers the fundamentals of Rust, including installation, language basics, and control flow. In the next section, we'll explore ownership and memory management in Rust.