diff --git a/code/src/week_2/task_1.rs b/code/src/week_2/task_1.rs index f47db00..057c945 100644 --- a/code/src/week_2/task_1.rs +++ b/code/src/week_2/task_1.rs @@ -1,11 +1,14 @@ +// Demonstrates functions, return types, and expression blocks +// Function that converts BTC to USD fn btc_value_in_usd(btc: f64, rate: f64) -> f64 { - btc * rate + // The expression below returns btc * rate without using 'return' or a semicolon + btc * rate } -//main run main + fn main() { - let btc = 0.25; //a dream amount - let rate = 65_000.0; // example rate here + let btc = 0.05; + let rate = 68_000.0; // Example: 1 BTC = $68,000 let value = btc_value_in_usd(btc, rate); println!("{} BTC is worth ${} USD", btc, value); } diff --git a/code/src/week_2/task_2.rs b/code/src/week_2/task_2.rs index aca8ad6..83ea422 100644 --- a/code/src/week_2/task_2.rs +++ b/code/src/week_2/task_2.rs @@ -1,18 +1,25 @@ + +// Demonstrates control flow, for loops, while loops, and conditionals + fn mine_blocks(limit: u8) { let mut difficulty = 1; for height in 1..=limit { - println!("Mining block #{}", height); + println!("Mining block #{} at difficulty {}", height, difficulty); - // Simulate difficulty - while difficulty < 3 { - println!("Simulating difficulty level {}", difficulty); - difficulty += 1; // the number of starting zero's + // Every 5 blocks, simulate a checkpoint + if height % 5 == 0 { + println!("Checkpoint reached at block #{}", height); } - if height % 5 == 0 { - println!("Checkpoint reached"); + // Simulate difficulty increase using a while loop + let mut work_done = 0; + while work_done < difficulty { + println!("Performing mining work... (step {})", work_done + 1); + work_done += 1; } + + difficulty += 1; // Increase difficulty for next round } } diff --git a/code/src/week_2/task_3.rs b/code/src/week_2/task_3.rs index 0b0b29a..ea2b41d 100644 --- a/code/src/week_2/task_3.rs +++ b/code/src/week_2/task_3.rs @@ -1,36 +1,32 @@ + +// Demonstrates enums, match statements, and returning string slices + enum Network { Mainnet, Testnet, Regtest, } -fn get_rpc_url(network: &Network) -> &str { +// Function that prints network details using match +fn print_network_info(network: &Network) { match network { - Network::Mainnet => "https://mainnet.example.com", - Network::Testnet => "https://testnet.example.com", - Network::Regtest => "http://localhost:8332", + Network::Mainnet => println!("You are connected to Bitcoin Mainnet "), + Network::Testnet => println!("You are connected to Bitcoin Testnet "), + Network::Regtest => println!("You are connected to Bitcoin Regtest "), } } -fn print_network_details(network: &Network) { +// Function that returns an RPC URL for each network +fn get_rpc_url(network: &Network) -> &str { match network { - Network::Mainnet => println!("This is the main Bitcoin network."), - Network::Testnet => println!("This is the test Bitcoin network."), - Network::Regtest => println!("This is the regtest Bitcoin network."), + Network::Mainnet => "https://mainnet.bitcoin.org/rpc", + Network::Testnet => "https://testnet.bitcoin.org/rpc", + Network::Regtest => "http://localhost:18443", } } fn main() { - let network = Network::Regtest; - - print_network_details(&network); - println!("RPC URL: {}", get_rpc_url(&network)); - let network = Network::Mainnet; - - print_network_details(&network); - println!("RPC URL: {}", get_rpc_url(&network)); let network = Network::Testnet; - - print_network_details(&network); + print_network_info(&network); println!("RPC URL: {}", get_rpc_url(&network)); }