# Week 1: Fundamentals ## Introduction to Rust Programming ## Agenda - Day 1: Introduction and Setup - Day 2: Memory Concepts and Basic Syntax - Day 3: Primitive Data Types and Exercises --- # Day 1: Introduction to Rust Programming ## What is Rust? * A systems programming language * Designed for performance, reliability, and productivity * Memory-safe and concurrency-safe * Growing in popularity for various applications ## Why Rust? * **Performance:** Close to C/C++ performance * **Reliability:** Prevents common programming errors like null pointer dereferencing and data races * **Productivity:** Strong type system, powerful tooling, and a supportive community ## Setting Up the Environment - Installing Rust - Visit rustup.rs - Unix: `curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh` - Windows: `choco install rust` - Verifying installation - Open terminal/command prompt - Run: `rustc --version` ## Basic Command-line Usage - `rustc`: Rust compiler - `cargo`: Rust's package manager and build system - `cargo new`: Create a new project - `cargo build`: Compile the project - `cargo run`: Run the project ## Your First Rust Program ```rust fn main() { println!("Hello, World!"); } ``` - Save as `hello.rs` - Compile: `rustc hello.rs` - Run: `./hello` (or `hello.exe` on Windows) ## Day 1 Summary - Installed Rust and set up the environment - Learned basic command-line tools: `rustc` and `cargo` - Created and ran our first Rust program # Day 2: Memory Concepts and Basic Syntax ## Introduction to Memory - Computer memory: refers to the electronic components used to store data and instructions that can be accessed quickly by the processor for various computing tasks. - Two main types: 1. RAM (Random Access Memory) 2. Storage (HDD, SSD) ## Memory Layout The memory layout of a running program is typically divided into the following segments: - **Stack**: Used to store local variables, function arguments, and return addresses during function calls. - **Heap**: Used for dynamic memory allocation at runtime. - **Data**: Stores global and static variables. - **Text**: Stores the executable code of the program. ## Deep Dive: Stack vs Heap ### Stack The stack is a specific area within your computer's memory where data is stored and managed in a Last-In-First-Out (LIFO) order. *It is primarily used for simple data structures with a known, fixed size at compile time.* - **Faster Access**: Accessing data on the stack is generally faster due to direct memory addressing by the stack pointer which is a register. - **Fixed Size**: The stack has a fixed size, which is determined at program startup. If you try to allocate more memory on the stack than is available, you'll get a stack overflow error. - **LIFO (Last-In-First-Out)**: The stack operates on a Last-In-First-Out principle. The last item pushed onto the stack is the first one to be popped off. - **Allocation**: function calls, storing local variables, function arguments, and return addresses. ### Heap The heap memory is a region of a process's memory that is used for dynamic memory allocation, memory blocks can be allocated and deallocated at runtime, as needed by the program. - **Dynamic Allocation**: Memory can be allocated and deallocated during program execution. - **Slower Access**: Accessing memory on the heap is generally slower than accessing memory on the stack due to the overhead of allocation and deallocation. - **Unstructured**: Unlike stack memory, heap memory is not organized in a specific order. - **Ownership and Borrowing**: Rust's ownership and borrowing system ensures memory safety and prevents common memory-related errors like data races and memory leaks. # Rust Syntax ## Variables - Variable declaration: `let x = 5;` - Mutability: `let mut y = 10;` - Constants: `const MAX_POINTS: u32 = 100_000;` ## Data Types - Scalar types: - Integers: `i32`, `u64`, etc. - Floating-point: `f32`, `f64` - Booleans: `bool` - Characters: `char` - Compound types: - Tuples: `(i32, f64, u8)` - Arrays: `[1, 2, 3, 4, 5]` ## Basic Operations - Arithmetic: `+`, `-`, `*`, `/`, `%` - Comparison: `==`, `!=`, `<`, `>`, `<=`, `>=` - Logical: `&&`, `||`, `!` ## Day 2 Summary - Explored memory concepts: Stack vs Heap - Introduced Rust syntax for variables and data types - Covered basic operations in Rust # Day 3: Primitive Data Types and Exercises ## Primitive Data Types - Integers (i32, u64, etc.) - Floating-point numbers (f32, f64) - Booleans (bool) - Characters (char) - String Slice (&str) - Tuples - Arrays --- ### Integers - Signed: `i8`, `i16`, `i32`, `i64`, `i128`, `isize` - Unsigned: `u8`, `u16`, `u32`, `u64`, `u128`, `usize` Example: ```rust let a: i32 = -15; let b: u32 = 42; ``` ### Floating Point Numbers - Single-precision: `f32` - Double-precision: `f64` Example: ```rust let f: f32 = 2.5; let pi: f64 = 3.142; ``` ### Character Represents a single Unicode character, enclosed in single quotes. Example: ```rust let a: char = 'a'; let beta: char = 'β'; let perfect: = '💯'; ``` ### Booleans - `bool` type: `true` or `false` - Used in conditional statements and logical operations Example: ```rust let is_rust_fun: bool = true; let is_rust_difficult = false; ``` ### String Slice - `&str`: string slice - Immutable reference to UTF-8 encoded string data - Often used for string literals Example: ```rust let greeting: &str = "Hello, Rust!"; ``` ### Tuple A fixed-size collection of elements of different types, enclosed in parentheses. Example: ```rust let tuple = (1, "hello", 3.14); ``` ### Arrays A fixed-size collection of elements of the same type, declared with a specific length. Example: ```rust let array = [1, 2, 3, 4, 5]; ``` --- ## Hands-on Exercises 1. Write a program to calculate the factorial of a number. 2. Create a program to check if a given number is prime. 3. Implement a simple guessing game. --- ## Day 3 Summary - Explored primitive data types in detail - Learned about string slices - Practiced with hands-on exercises --- # Week 1 Wrap-up - Set up Rust development environment - Learned about memory concepts - Explored Rust syntax and primitive data types - Next week: Control flow and functions --- # Thank You! ## Any Questions?