Featured image of post How to Create a Program to Enumerate Prime Numbers in Rust and Code Examples

How to Create a Program to Enumerate Prime Numbers in Rust and Code Examples

Introduces an implementation example of a simple algorithm to enumerate prime numbers up to a specified upper limit as a learning exercise for Rust programming. Explains the basic coding method using loops and conditional branching clearly with specific sample code.

I wrote a program to enumerate prime numbers in Rust.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
fn main() {
	let max = 1000;
    let mut primes = vec![2];
    let mut n = 3;
    loop {
        let mut is_prime = true;
        for p in &primes {
            if n % p == 0 {
                is_prime = false;
                break;
            }
        }
        if is_prime {
            primes.push(n);
        }
        n += 2;
		if n > max {
			break;
		}
    }
    for p in &primes {
		println!("{}", p);
    }
}