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
11 changes: 7 additions & 4 deletions code/src/week_2/task_1.rs
Original file line number Diff line number Diff line change
@@ -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);
}
21 changes: 14 additions & 7 deletions code/src/week_2/task_2.rs
Original file line number Diff line number Diff line change
@@ -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
}
}

Expand Down
32 changes: 14 additions & 18 deletions code/src/week_2/task_3.rs
Original file line number Diff line number Diff line change
@@ -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));
}