Rust – Fast & Safe System Programming

Guessing Game

main.rs — huthegeek.com
use std::io;
use rand::Rng;
use std::cmp::Ordering;

fn main() {
    println!("Guess the number (1..100)!");

    let secret_number = rand::thread_rng().gen_range(1..=100);

    // println!("The secret number is: {secret_number}");
    loop {
        println!("Please input your guess.");

        let mut guess = String::new();

        io::stdin()
            .read_line(&mut guess)
            .expect("Failed to read line");
        let guess: u32 = match guess.trim().parse() {
            Ok(num) => num,
            Err(_) => continue,
        };
        println!("You guessed: {guess}");
        match guess.cmp(&secret_number) {
            Ordering::Less => println!("Too small!"),
            Ordering::Greater => println!("Too big!"),
            Ordering::Equal => {
                println!("You win!");
                break;
            }
        }
    }
}
John@DESKTOP-V7D87B MINGW64 ~/src/rust/guess (master)
Click ‘Run’ to compile and play…

Mandelbrot – Math Magician

The Mathematical Magician: Mandelbrot

While the Guessing Game introduces basic logic, the Mandelbrot set scales that complexity into the infinite. By iterating the simple formula zn+1 = zn2 + c across millions of coordinates, we transform abstract math into visual art.

Showcasing Rust’s Real Power

This is where we unleash the 8 threads of the i5-8250U. Using the Rayon crate, we perform “Fearless Concurrency”—splitting millions of coordinate calculations across the CPU simultaneously.

// Transform a standard loop into a parallel powerhouse
img.enumerate_rows_mut()
  .par_bridge() // Rayon handles thread scheduling
  .for_each(|(y, row)| {
    for (x, _y, pixel) in row {
      let c = pixel_to_complex(x, y, width, height);
      // Escape-time decision: bail after n iterations
      let intensity = mandelbrot_intensity(c, max_iter);
      *pixel = Rgb([intensity, intensity, intensity]);
    }
  });

Despite the “timid” nature of a mobile processor, Rust’s zero-cost abstractions allow it to outperform expectations. Here is how the laptop handled 1080p renders at 1,000 iterations:

Render Preset Complexity 8-Thread Time
Full Set Low 0.42s
Seahorse Valley Medium 1.15s
Triple Spiral High (Deep Zoom) 2.84s

Rust’s strict memory safety ensures that even with this massive parallelism, the application remains stable with zero data races.

Delving into the Art: High-Performance Exploration

To bring systems-level performance to the web, the Rust source code is compiled into WebAssembly (WASM). Using the wasm-bindgen crate, we create a high-speed bridge that executes complex coordinate math at near-native speeds directly on your local hardware—even your phone!

Initializing WASM Engine…

The Technical Advantage: Because Rust manages memory manually and safely, it prevents the “stutter” often found in standard JavaScript during deep zooms. By leveraging WASM, this engine delivers lightning-fast navigation across all devices, proving that enterprise-grade logic is truly portable and incredibly efficient.

Note: Drag to pan the view. On mobile, tap to zoom in and long-press (1 sec) to zoom out.