Hi, I am a Rust newbie.

I played with it and made a prime number generator - up to the values of ~10^9 .

I made the same program in Node. I was amazed when Node program was faster. For values up to 10^7 it took Rust 14.6s and Node 4.8s.

The code is provided bellow. I could change the position of sqrt check but that's not the point.

1st argument is the limit up to which primes are generated - 10000000 for the times mentioned before

Rust code - rustc 1.43.0 (4fb7144ed 2020-04-20)

```
use std::env;
use std::time::Instant;
use std::fs::File;
use std::io::Write;
fn main() {
let args: Vec<String> = env::args().collect();
let limit: u32 = args[1].parse().unwrap();
if(limit > u32::MAX - 1) {
println!("Input limited to u32::MAX - 1 = 4_294_967_294u32");
return;
}
let mut primes: Vec<u32> = Vec::new();
primes.push(2);
let mut i: u32 = 3;
let mut j: u32 = 0;
let start = Instant::now();
loop {
if(i > limit) { break; }
let mut factor: u32 = 0;
let sqrt: f64 = (i as f64).sqrt();
let sqrt_u32: u32 = sqrt.ceil() as u32;
let len: u32 = (primes.len() as u32) - 1;
loop {
if(j > len) { break; }
let prime: u32 = primes[j as usize];
let modulo: u32 = i % prime;
if(modulo < 1) {
factor = prime;
break;
}
// if((prime as f64) > sqrt) { break; }
if(prime > sqrt_u32) { break; }
j = j + 1;
}
j = 0;
if(factor < 1) {
// new prime found
primes.push(i);
}
if(i%1000000 < 1) { println!("Logging {}", i.to_string()); }
i = i + 1;
}
let duration = start.elapsed();
println!("Duration: {:?}", duration);
// print
/*
for i in 0..=primes.len() - 1 {
println!("{}", primes[i].to_string());
}
*/
// write
let file_name: String = format!("{}.txt", limit.to_string());
let mut file = File::create(file_name).expect("Unable to create file");
write!(file, "Limit: {}\nFound: {}\nDuration: {:?}\n[", limit.to_string(), primes.len().to_string(), duration);
for prime in &primes {
write!(file, "{}, ", prime);
}
}
```

Node.js code - Node v12.16.2

```
let fs = require('fs');
const args = process.argv.slice(2)
let limit = args[0] || 100;
let primes = [2];
let start = Date.now();
for(let i = 3; i <= limit; i++) {
// check if i has any factors among prior primes
let factor = -1;
let sqrt = Math.sqrt(i);
for(j = 0; j < primes.length; j++) {
let prime = primes[j]
let mod = i % prime;
if(mod < 1) {
factor = prime;
break;
}
if(prime > sqrt) break;
}
if(factor < 0) {
// new prime found
primes.push(i);
}
if(i%1000000 < 1) console.log('Logging', i);
}
let duration = Date.now() - start;
let string = 'Limit: ' + limit + '\n';
string += 'Found: ' + primes.length + '\n';
string += 'Duration: ' + duration + '\n';
string += JSON.stringify(primes);
fs.writeFileSync(limit + '.txt', string);
```

Is there anything fundamentally wrong that I am doing in my rust code so that performance is bad?

Thank you for your time