Rust
Rust is a modern and statically-typed programming language that focuses on performance, safety, and concurrency. It aims to provide memory safety without sacrificing performance, making it suitable for systems programming, embedded systems, and performance-critical applications. Rust's key features include its ownership system, which enforces strict rules to prevent memory-related bugs like null pointer dereferences and data races.
Module 1: Introduction to Rust
Introduction to Rust
Rust is a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety.
Variables and Control Flow
Rust is statically typed and variables are immutable by default. It has various control flow operators such as if, while, for, match (similar to switch in other languages).
fn main() { let x = 5; if x == 5 { println!("x is five!"); } }
Functions
Functions in Rust are defined with the keyword 'fn' and can return a value.
fn add(x: i32, y: i32) -> i32 { return x + y; } fn main() { let result = add(5, 7); println!("The sum is {}", result); }
Module 2: Structs and Enums in Rust
Structs
Structs are similar to classes in other languages and are defined with the keyword 'struct'. They are used to create complex data types.
struct Person { name: String, age: u8, } fn main() { let person = Person { name: String::from("Alice"), age: 30, }; println!("{} is {} years old", person.name, person.age); }
Enums
Enums, short for enumerations, allow you to define a type by enumerating its possible variants.
enum Message { Quit, Move { x: i32, y: i32 }, Write(String), ChangeColor(i32, i32, i32), } fn main() { let msg = Message::Write(String::from("hello")); match msg { Message::Write(text) => println!("Message text: {}", text), _ => {}, } }
Module 3: Error Handling in Rust
Error Handling
Rust groups errors into two major categories: recoverable and unrecoverable errors. For recoverable errors, Rust has the Result<T, E> type and for unrecoverable errors, it has the panic! macro.
use std::fs::File; fn main() { let f = File::open("file.txt"); let f = match f { Ok(file) => file, Err(error) => panic!("Problem opening the file: {:?}", error), }; }
Module 4: Concurrency in Rust
Threads
Rust's standard library provides a library for threads, which allow parallel execution of code. To make a new thread, we call the thread::spawn function and pass it a closure containing the code we want to run in the new thread.
use std::thread; use std::time::Duration; fn main() { let handle = thread::spawn(|| { for i in 1..10 { println!("hi number {} from the spawned thread!", i); thread::sleep(Duration::from_millis(1)); } }); handle.join().unwrap(); }
Message Passing
Rust has a message-passing concurrency feature where threads communicate by sending each other messages. This is done through channels.
use std::sync::mpsc; use std::thread; fn main() { let (tx, rx) = mpsc::channel(); thread::spawn(move || { let val = String::from("hi"); tx.send(val).unwrap(); }); let received = rx.recv().unwrap(); println!("Got: {}", received); }