Internet Made Me Learn Rust

02 Dec 24

rustprogramminglanguage

Lately, I’ve been noticing this programming language mentioned here and there. Not very often, but enough to catch my attention. I read that it’s a language without a garbage collector but with a design that ensures memory safety. This seemed quite interesting, so I’d been waiting to find some time to explore it further.

One day, I got up early, had some free time, and decided to dive into it. I turned on my laptop and started learning Rust, experimenting with its features. I installed rustup on my Windows system, which is the tool used to set up Rust and cargo, Rust’s package manager.

Learning a new programming language is always intriguing. I was eager to pick it up quickly, so I decided to write a simple prime number generator. I headed over to Rust’s documentation and their playground. There, I learned about some basic concepts like struct, traits, impl, and other aspects of Rust’s syntax. I also learned about the fn main() function, which is the entry point of a program, just like in C or C++. Since I already knew these languages, Rust felt quite intuitive to me.

I managed to write a prime number generator using the Sieve of Eratosthenes algorithm. I ran it using cargo run main.rs. Boom! I encountered several compilation errors complaining about borrowing or cloning some objects.

After some investigation and with the help of AI, I was able to fix the code and run it successfully. It only took about 200ms to generate primes up to 1,000,000, compared to Python, which took approximately 1.2 seconds.

Next, I tried optimizing the prime generator further using bit operations. I implemented a bitwise sieve to push the upper limit of prime number generation to 100,000,000.

Here’s the code if you’re curious:

// Set the bit at the given position to 1
fn set_pos(arr: &mut [u32], pos: usize) {
    let block_index: usize = pos / 32; // Calculate the block index (Each block is 32 bits)
    let bit_index: usize = pos % 32; // Calculate the bit index
    arr[block_index] |= 1 << bit_index; // Set the bit at the given position to 1
}

// Get the bit at the given position
fn get_pos(arr: &[u32], pos: usize) -> bool {
    let block_index = pos / 32;
    let bit_index = pos % 32;
    (arr[block_index] & (1 << bit_index)) != 0
}

// The bitwise Sieve of Eratosthenes algorithm
fn sieve_of_eratosthenes(n: usize) {
    let mut is_prime: Vec<u32> = vec![0; (n >> 5) + 1];
    is_prime[0] = 0;
    is_prime[1] = 0;

    // Set 2 as prime
    set_pos(&mut is_prime, 2);

    // Start from 3 and skip even numbers
    for i in (3..=n).step_by(2) {
        if !get_pos(&is_prime, i) {
            let start = i * i;

            for j in (start..=n).step_by(i * 2) {
                set_pos(&mut is_prime, j);
            }
        }
    }
}

I already started devloping a desktop app using Rust and Tauri. And plan to write more about it in the future.

So far, so good! I love this language and will continue exploring Rust to delve deeper into its core.

© 2025 Written by Md Nahid Hasan