Rust Programming Tutorial

Welcome to the Rust programming tutorial! Learn the fundamentals of Rust and get started with your first program.

Lesson 1: Hello World

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

This is the first Rust program. It prints "Hello, world!" to the console.

Lesson 2: Variables and Types

let age = 25; // integer
let pi = 3.14159; // floating-point

Rust uses type inference to determine the type of variables. You can declare variables explicitly or let the compiler infer their types.

Lesson 3: Data Structures

Data structures are essential for organizing and manipulating data in Rust programs.

Lesson 4: Control Flow

if condition { ... } else { ... }
while loop { ... }
for loop { ... }

Control flow statements allow you to execute blocks of code based on certain conditions.

Lesson 5: Functions

fn greet(name: &str) { println!("Hello, {}", name); }

Functions are used to organize code and reuse it in different parts of a program.

Lesson 6: Error Handling

use std::io::{Result, Read};

Error handling is crucial for building robust and reliable applications in Rust.

Lesson 7: Ownership

let s1 = String::from("hello");

Rust's ownership system ensures safe and efficient memory management by tracking where each piece of data lives and who owns it.

Lesson 8: Traits

impl Trait for Type { fn method() {} }

Traits enable code reuse and abstraction by defining common behaviors for related types.

Lesson 9: Lifetimes

struct MyData<'a> { value: &'a str }

Lifetimes are used to ensure that references are valid for the duration they're used.

Lesson 10: Conclusion

Rust is a powerful and safe language suitable for systems programming and web development. By learning the basics, you can build complex applications with confidence.