# π¦ Rust Master Class - Chapter 1: Introduction to Rust
I felt old in this body. Not from age β from languages that let me get away with programming blunder. Then Rust showed up. It was like switching from automatic to manual transmission: suddenly I understood how the car actually worked.
Rust is a systems programming language that focuses on safety, speed, and concurrency . Its most fundamental basics include concepts found in many languagesβsuch as variables, data types, functions, and control flowβbut it implements them with a unique focus on memory safety .
What Makes Rust Unique?
The defining feature of Rust is its approach to memory management. Unlike languages that use a garbage collector (GC) to clean up memory at runtime or those that require the programmer to manually allocate and free memory, Rust uses a system of ownership with rules that the compiler checks at compile time . This ensures that programs are safe from common bugs like "double free" errors or "dangling pointers" without the performance penalty of a GC .
Key Concepts and Code Examples
1. Ownership and Move Semantics
Ownership is governed by three rules: each value has an owner, there can only be one owner at a time, and the value is dropped when the owner goes out of scope . When data on the heap (like a String) is assigned to another variable, Rust moves the ownership rather than copying the data to ensure memory safety .
fn main() {
// Create a new variable
let text = String::from("hello");
// Create a new variable
let copied_text = text; // text is MOVED to copied_text; text is no longer valid
// Output to console
// println!("{}", text); // This would cause a compile-time error
// Output to console
println!("{}", copied_text); // This works
}
2. Borrowing and References
To use a value without taking ownership, Rust uses references. This process is called borrowing . References can be immutable (default) or mutable (using &mut), but you cannot have a mutable reference while immutable ones are active .
fn main() {
// Create a new variable
let text = String::from("hello");
// Create a new variable
let len = calculate_length(&text); // Borrowing text via a reference
// Output to console
println!("The length of '{}' is {}.", text, len);
}
fn calculate_length(s: &String) -> usize { // s is a reference to a String
s.len()
}
3. Structs and Methods
Structs allow you to package related data together into a custom type . Methods are functions defined within the context of a struct to specify behavior associated with that type .
struct Student {
name: String,
age: u8,
}
impl Student {
fn some_fn_1(&self) -> String {
format!("Student: {}", self.name)
}
}
4. Generics and Traits
Generics are abstract stand-ins for concrete types, allowing you to write code that handles multiple types without duplication . Traits define shared behavior that different types can implement, similar to interfaces in other languages .
// A generic function that works for any type T that can be multiplied
fn square<T>(value: T) -> T
where T: std::ops::Mul<Output = T> + Copy {
value * value
}
trait GeneralInfo {
fn info(&self) -> (&str, u8, char);
}
5. Lifetimes
Lifetimes are a specialized form of generics that provide the compiler with information about how references relate to each other, ensuring that a reference remains valid for as long as it is needed .
// 'a specifies that the returned reference will last at least as long as text
fn return_str<'a>(first: &'a str) -> &'a str {
first
}
Common Programming Basics
- Variables and Mutability: Variables are immutable by default; use
mutto make them changeable . - Data Types: Includes scalars like integers (
i32), Booleans (bool), and floating-point numbers, as well as compounds like tuples and arrays [27-29]. - Control Flow: Uses
ifexpressions and loops (loop,while,for) to manage execution .
π Download the full PDF: Coming soon
Part 1 of the Rust Master Class series β STEM EdTech | Automation Consulting | Rust Tutoring
Top comments (0)