diff --git a/README.md b/README.md index f454b2c..2708037 100644 --- a/README.md +++ b/README.md @@ -2,18 +2,24 @@ Welcome! This repository documents my journey of learning the Rust programming language. The goal is to create a clear, step-by-step resource that others can also use to learn Rust. + + ## Folder Structure -- `topics/`: Deep dives into specific Rust concepts (Ownership, Borrowing, etc.). -- `examples/`: Runnable code examples related to each topic. -- `cargo_setup.md`: A guide on how to get started with Cargo. +- `src/TOPICS/(Topic)/README.md`: Deep dives into specific Rust concepts (Ownership, Borrowing, etc.). +- `src/TOPICS/(Topic)/snippets/`: Runnable code examples related to each topic. +- `src/TOPICS/SETTING-UP/cargo_setup.md`: A guide on how to get started with Cargo. +I ADVISE YOU READ THE TOPICS IN THE ORDER IN WHICH THEY ARE NUMBERED. ## Current Topics -- [Ownership & Memory Management](topics/ownership/README.md) +1. [Setting-Up](/src/TOPICS/1_Setting-Up) +2. [Ownership & Memory Management](/src/TOPICS/2_Ownership) ## Contributors +If you'd love to contribute, go to [CONTRIBUTING](CONTRIBUTING.md) + Special thanks to those who have contributed their time and knowledge to this documentation: - **Obot (@oboobotenefiok)**: Provided detailed explanations on Ownership rules and memory management. diff --git a/examples/ownership/move_error.rs b/examples/ownership/move_error.rs deleted file mode 100644 index 9309d99..0000000 --- a/examples/ownership/move_error.rs +++ /dev/null @@ -1,18 +0,0 @@ -/// [INTENTIONAL ERROR] Example: The "Move" Rule in Rust Ownership -/// Inspired by @oboobotenefiok -/// -/// This file will NOT compile. It demonstrates what happens when you try to -/// use a variable after its value has been moved. - -fn main() { - // 1. indie_hacker owns the String "Caleb" - let indie_hacker = String::from("Caleb"); - - // 2. OWNERSHIP MOVES from indie_hacker to underdog_builder - let underdog_builder = indie_hacker; - - // 3. ERROR! indie_hacker no longer owns the data. - println!("This will throw error? {}", indie_hacker); - - println!("New owner is: {}", underdog_builder); -} diff --git a/examples/ownership/move_fixed.rs b/examples/ownership/move_fixed.rs deleted file mode 100644 index dead8ce..0000000 --- a/examples/ownership/move_fixed.rs +++ /dev/null @@ -1,17 +0,0 @@ -/// [FIXED] Example: Handling Moved Values via Cloning -/// Inspired by @oboobotenefiok -/// -/// This file WILL compile. It demonstrates how to keep the original variable -/// valid by creating a deep copy (clone) of the data. - -fn main() { - let indie_hacker = String::from("Caleb"); - - // Use .clone() to create a deep copy on the heap. - // Now both variables own their own distinct "Caleb" string. - let underdog_builder = indie_hacker.clone(); - - // Both are valid! - println!("Original: {}", indie_hacker); - println!("Clone: {}", underdog_builder); -} diff --git a/examples/ownership/scope_dropped.rs b/examples/ownership/scope_dropped.rs deleted file mode 100644 index 9f45863..0000000 --- a/examples/ownership/scope_dropped.rs +++ /dev/null @@ -1,14 +0,0 @@ -/// Example: Variable Dropping and Scope -/// Inspired by @oboobotenefiok - -fn main() { - status(); - - // println!("{}", indie_hacker); // ERROR: would not compile here -} - -fn status() { - // indie_hacker is local to this block - let indie_hacker = String::from("Caleb"); - println!("Inside status: {}", indie_hacker); -} // indie_hacker is DROPPED here. Memory is freed. diff --git a/examples/ownership/stack_copy.rs b/examples/ownership/stack_copy.rs deleted file mode 100644 index cf00569..0000000 --- a/examples/ownership/stack_copy.rs +++ /dev/null @@ -1,16 +0,0 @@ -/// Example: Stack-based Data (Copy vs Move) -/// Inspired by @oboobotenefiok - -fn main() { - // Simple values with a fixed size (integers, bools, chars, &str) - // are stored on the STACK. They implement the 'Copy' trait. - - let x = "Caleb"; // Simple string literal (&str) - let y = x; // Copied automatically, not moved. - - println!("Stack values are copied: x = {}, y = {}", x, y); - - let a = 10; - let b = a; // Copied - println!("Integers too: a = {}, b = {}", a, b); -} diff --git a/main.rs b/main.rs deleted file mode 100644 index 8d4322f..0000000 --- a/main.rs +++ /dev/null @@ -1,12 +0,0 @@ -fn main() { - status(); -} - -fn status() { - let indie_hacker = String::from("Caleb"); - - let underdog_builder = indie_hacker; - - println!("This is the name: {}", underdog_builder); - -} diff --git a/cargo_setup.md b/src/TOPICS/1_Setting-Up/README.md similarity index 100% rename from cargo_setup.md rename to src/TOPICS/1_Setting-Up/README.md diff --git a/topics/ownership/README.md b/src/TOPICS/2_Ownership/README.md similarity index 100% rename from topics/ownership/README.md rename to src/TOPICS/2_Ownership/README.md diff --git a/src/TOPICS/2_Ownership/snippets/main.rs b/src/TOPICS/2_Ownership/snippets/main.rs new file mode 100644 index 0000000..70d8cb4 --- /dev/null +++ b/src/TOPICS/2_Ownership/snippets/main.rs @@ -0,0 +1,103 @@ + fn main() { + println!("Welcome to the Rust Learning Journey!"); + println!("Check the 'topics' folder for explanations and 'examples' for code. \n Explanatios are named README while examples are in SNIPPETS"); + start(); + } + + fn start() { + status(); +} + +fn status() { + let indie_hacker = String::from("Caleb"); + + let underdog_builder = indie_hacker; + + println!("This is the name: {}", underdog_builder); + +} + + +/// Example: Stack-based Data (Copy vs Move) + +/// Inspired by @oboobotenefiok + + +fn main() { + // Simple values with a fixed size (integers, bools, chars, &str) + // are stored on the STACK. They implement the 'Copy' trait. + + let x = "Caleb"; // Simple string literal (&str) + let y = x; // Copied automatically, not moved. + + println!("Stack values are copied: x = {}, y = {}", x, y); + + let a = 10; + let b = a; // Copied + println!("Integers too: a = {}, b = {}", a, b); + +} + + +/// Example: Variable Dropping and Scope + +/// Inspired by @oboobotenefiok + + +fn main() { + status(); + + // println!("{}", indie_hacker); // ERROR: would not compile here +} + +fn status() { + // indie_hacker is local to this block + let indie_hacker = String::from("Caleb"); + println!("Inside status: {}", indie_hacker); +} // indie_hacker is DROPPED here. Memory is freed +. + + +/// [FIXED] Example: Handling Moved Values via Cloning + +/// Inspired by @oboobotenefiok + +/// +/// This file WILL compile. It demonstrates how to keep the original variable +/// valid by creating a deep copy (clone) of the data. + +fn main() { + let indie_hacker = String::from("Caleb"); + + // Use .clone() to create a deep copy on the heap. + // Now both variables own their own distinct "Caleb" string. + let underdog_builder = indie_hacker.clone(); + + // Both are valid! + println!("Original: {}", indie_hacker); + println!("Clone: {}", underdog_builder); + +} + + +/// [INTENTIONAL ERROR] Example: The "Move" Rule in Rust Ownership + +/// Inspired by @oboobotenefiok + +/// +/// This file will NOT compile. It demonstrates what happens when you try to +/// use a variable after its value has been moved. + +fn main() { + // 1. indie_hacker owns the String "Caleb" + let indie_hacker = String::from("Caleb"); + + // 2. OWNERSHIP MOVES from indie_hacker to underdog_builder + let underdog_builder = indie_hacker; + + // 3. ERROR! indie_hacker no longer owns the data. + println!("This will throw error? {}", indie_hacker); + + println!("New owner is: {}", underdog_builder); + +} diff --git a/src/main.rs b/src/main.rs index 9a3deb9..e69de29 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +0,0 @@ - fn main() { - println!("Welcome to the Rust Learning Journey!"); - println!("Check the 'topics' folder for explanations and 'examples' for code."); - } \ No newline at end of file