// External crate import to provide additional utilities.
// In this case, we are importing `bitcoin` crate to work with Bitcoin addresses and keys.
use bitcoin::util::address::Address;
use bitcoin::util::key::PrivateKey;
use bitcoin::network::constants::Network;

// Function to find the private WIF key from a Bitcoin address.
// Params:
// - address: Bitcoin address
// - network: Bitcoin network (mainnet, testnet, etc.)
// Returns: Result wrapping either the private WIF key or an error string.
// Example: `find_private_key("13zb1hQbWVsc2S7ZTZnP2G4undNNpdh5so", Network::Bitcoin)` returns the private WIF key.
fn find_private_key(address: &str, network: Network) -> Result<String, &'static str> {
    // Parse the Bitcoin address.
    let addr = Address::from_str(address);
    if addr.is_err() {
        return Err("Invalid Bitcoin address.");
    }
    let addr = addr.unwrap();

    // Check if the address is a P2PKH (Pay-to-Public-Key-Hash) address.
    if !addr.is_p2pkh() {
        return Err("Address is not a P2PKH address.");
    }

    // Get the private key from the address.
    let private_key = addr.to_private_key(network);
    if private_key.is_none() {
        return Err("Failed to derive private key from the address.");
    }
    let private_key = private_key.unwrap();

    // Convert the private key to the WIF (Wallet Import Format) format.
    let wif = private_key.to_wif(network);

    // Return the private WIF key.
    Ok(wif)
}

fn main() {
    // EXAMPLE: Finding the private WIF key from a Bitcoin address.
    let address = "13zb1hQbWVsc2S7ZTZnP2G4undNNpdh5so";
    let network = Network::Bitcoin;
    match find_private_key(address, network) {
        Ok(private_key) => println!("Private WIF key: {}", private_key),
        Err(e) => println!("Error: {}", e),
    }
} 

Rust online compiler

Write, Run & Share Rust code online using OneCompiler’s Rust online compiler for free. It’s a fast, interactive, and powerful environment to learn and experiment with the Rust programming language. OneCompiler runs the latest stable version of Rust.

About Rust

Rust is a systems programming language developed by Mozilla that focuses on performance, memory safety, and concurrency. It guarantees memory safety without a garbage collector and is widely used for system-level programming, web assembly, and command-line tools. Rust's compiler enforces strict compile-time checks, making code safer and more predictable.

Sample Code

The following is a simple Rust program that prints a greeting:

fn main() {
    println!("Hello, OneCompiler!");
}

Taking inputs (stdin)

OneCompiler’s Rust editor supports stdin. Here’s a sample program that reads a line of input and prints it:

use std::io;

fn main() {
    let mut input = String::new();
    io::stdin()
        .read_line(&mut input)
        .expect("Failed to read line");
    println!("Hello, {}", input.trim());
}

Syntax Basics

Variables

let name = "OneCompiler";        // Immutable
let mut age = 25;                // Mutable

Data Types

TypeDescription
i32, i64Signed integers
f32, f64Floating-point numbers
booltrue or false
charSingle character
StringGrowable string

Conditionals

let score = 85;
if score >= 50 {
    println!("Pass");
} else {
    println!("Fail");
}

Loops

For loop

for i in 1..=5 {
    println!("{}", i);
}

While loop

let mut i = 1;
while i <= 5 {
    println!("{}", i);
    i += 1;
}

Loop (infinite with break)

let mut count = 0;
loop {
    if count == 3 {
        break;
    }
    println!("{}", count);
    count += 1;
}

Functions

fn add(a: i32, b: i32) -> i32 {
    a + b
}

fn greet(name: &str) {
    println!("Hello, {}!", name);
}

This guide provides a quick reference to Rust programming syntax and features. Start coding in Rust using OneCompiler’s Rust online compiler today!