Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions submissions/devfoma/week-2/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions submissions/devfoma/week-2/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "Week-2"
version = "0.1.0"
edition = "2024"

[dependencies]
Binary file added submissions/devfoma/week-2/public/tasks.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions submissions/devfoma/week-2/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
mod task_1;
mod task_2;
mod task_3;

fn main() {
// Example usage of task_1 function
let btc_amount = 0.5;
let exchange_rate = 30000.0; // Example exchange rate: 1 BTC = 30,000 USD

// calling the function from task_1 module
let usd_value = task_1::btc_value_in_usd(btc_amount, exchange_rate);
println!("The value of {} BTC in USD is: ${}", btc_amount, usd_value);

// Example usage of task_2 function
task_2::mine_blocks(12);

// Example usage of task_3 function
let rpc_url = task_3::get_rpc_url(&task_3::Network::Mainnet);
println!("RPC URL for Mainnet: {}", rpc_url);


}
14 changes: 14 additions & 0 deletions submissions/devfoma/week-2/src/task_1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// 1. Functions and Expressions
// - Write a function `btc_value_in_usd(btc: f64, rate: f64) -> f64` that returns btc * rate.

// Demonstrate:
// - Function return types
// - Expression blocks (no semicolons for return)
// made the function public to be accessible from main.rs

// My Solution:
#![allow(unused)]

pub fn btc_value_in_usd(btc: f64, rate: f64) -> f64 {
btc * rate
}
27 changes: 27 additions & 0 deletions submissions/devfoma/week-2/src/task_2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// 2. Control Flow & Loops
// Write a function that simulates mining blocks:

// ```
// fn mine_blocks(limit: u8) {
// for height in 1..=limit {
// println!("Mining block #{}", height);
// }
// }
// ```
// - Extend it using:
// - A while loop to simulate difficulty
// - An if-else block to print “Checkpoint reached” every 5 blocks.

// My solution:
#![allow(unused)]

pub fn mine_blocks(limit: u8) {
let mut height = 1;
while height <= limit {
println!("Mining block #{}", height);
if height % 5 == 0 {
println!("Checkpoint reached at block #{}", height);
}
height += 1;
}
}
31 changes: 31 additions & 0 deletions submissions/devfoma/week-2/src/task_3.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// 3. Enums & Pattern Matching
// Refer to this enum `Network`
// ```
// enum Network {
// Mainnet,
// Testnet,
// Regtest,
// }
// ```

// - Write a `match` block that prints details about the selected network.
// - Implement a function `fn get_rpc_url(network: &Network) -> &str` that returns different URLs.


// My Solution:

#![allow(unused)]

pub enum Network{
Mainnet,
Testnet,
Regtest,
}

pub fn get_rpc_url(network: &Network) -> &str {
match network{
Network::Mainnet => "https://mainnet.rpc.url",
Network::Testnet => "https://testnet.rpc.url",
Network::Regtest => "http://localhost:18443",
}
}
95 changes: 95 additions & 0 deletions submissions/devfoma/week-2/summary.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@

# Week 2 — Rust Fundamentals: Reflection and Summary

## Project Structure

This week’s assignment was organized into a modular Rust project, where each task was implemented in a separate file for clarity and reusability.

```
src/
├── main.rs
├── task_1.rs
├── task_2.rs
└── task_3.rs
```

**main.rs** — Serves as the program entry point. Integrates all tasks and demonstrates function calls from each module.
**task_1.rs** — Contains a function for calculating the value of Bitcoin in USD.
**task_2.rs** — Implements control flow and looping logic to simulate mining blocks and reaching checkpoints.
**task_3.rs** — Defines an enum and a pattern-matching function for selecting and returning network configurations.

This modular setup ensured clean separation of concerns, easy debugging, and scalability as more features are introduced in future weeks.

### my implementation for all tasks

![Screenshot for Week 2 Tasks](public/tasks.png "Tasks Output")


## Reflection: Summary of Learning and Debugging Process

### What I Learned

This week focused on developing a deeper understanding of **Rust fundamentals** through practical, Bitcoin-oriented exercises.
I learned how to structure a Rust project modularly using multiple files, apply control flow, and implement functions and enums effectively.

Specifically, I learned how to:

* Define and call functions with parameters and return types.
* Use **expression-based returns** to write concise and efficient code.
* Apply **loops** and **conditional logic** to simulate real-world processes such as block mining.
* Structure code across multiple files using `mod` and `pub` for modularity and reusability.
* Implement and use **enums** with **pattern matching** to manage different system states and configurations.
* Organize logic in a clean, readable way while maintaining functional separation between modules.

By completing these tasks, I gained confidence in writing Rust code that is both functional and scalable, while also reinforcing the importance of modular architecture in system-level development.



### Challenges Faced

1. **Understanding the `mod` and `pub` Relationship**

* Initially, I encountered visibility errors when trying to call functions defined in other files.
* **Cause:** Functions were not marked as `pub`, making them private to their modules.
* **Solution:** Used the `pub` keyword to make functions public and accessible across files, and ensured that `mod filename;` was correctly declared at the top of `main.rs`.

2. **Borrowing and Ownership in Function Calls**

* Some initial confusion occurred when passing references (`&`) versus owned values.
* **Cause:** Functions like `get_rpc_url()` expected borrowed references, not owned data.
* **Solution:** Reviewed Rust’s ownership and borrowing rules and passed references using `&` when necessary to satisfy the borrow checker.

3. **Debugging Module Import Errors**

* Encountered `unresolved import` and `cannot find module` errors during file organization.
* **Cause:** File names and paths did not match the module declarations in `main.rs`.
* **Solution:** Ensured all task files (`task_1.rs`, `task_2.rs`, `task_3.rs`) were correctly located in the `src/` directory and declared with matching module names.

4. **Output Formatting and Readability**

* Initially, printed outputs from different tasks looked disorganized.
* **Solution:** Added clear print statements and separators to distinguish outputs from each task, improving readability during testing.



### Debugging Approach

Throughout the exercises, I focused on understanding **why** errors occurred rather than just fixing them quickly.
My debugging strategy included:

* Reading compiler error messages carefully to identify the root cause.
* Checking for visibility and scope issues when working across modules.
* Running `cargo check` frequently to validate the project before execution.
* Making small, incremental changes and verifying outputs after each modification.
* Consulting the official Rust documentation and examples to confirm syntax and best practices.

This systematic approach helped me become more confident in reading compiler feedback, structuring projects, and managing modular dependencies effectively.


### Key Takeaway

This week’s exercises were instrumental in building a strong foundation for developing Bitcoin-related systems in Rust.
By implementing functions, control flows, and enums within a modular architecture, I now understand how to design structured, maintainable Rust applications.

The process improved my ability to debug logically, manage visibility across modules, and structure programs that can scale into larger projects.
Most importantly, it bridged the gap between theoretical syntax learning and **practical system implementation**, setting the stage for more advanced topics such as **ownership, borrowing, and data structures** in the coming weeks.