slovo/benchmarks/parse-loop/rust/parse_loop.rs
2026-05-22 08:38:43 +02:00

45 lines
983 B
Rust

const LOOP_COUNT: i32 = 1_000_000;
const EXPECTED_CHECKSUM: i32 = 345_000_001;
fn configured_loop_count() -> i32 {
let mut input = String::new();
if std::io::stdin().read_line(&mut input).is_err() {
return LOOP_COUNT;
}
input
.trim()
.parse::<i32>()
.ok()
.filter(|value| *value > 0)
.unwrap_or(LOOP_COUNT)
}
fn parse_text() -> String {
std::env::args().nth(1).unwrap_or_else(|| "12345".to_string())
}
fn parse_loop(limit: i32, text: &str) -> i32 {
let mut i = 0;
let mut acc = 1;
while i < limit {
acc += text.parse::<i32>().unwrap();
acc = if acc > 1_000_000_000 {
acc - 1_000_000_000
} else {
acc
};
i += 1;
}
acc
}
fn main() {
let text = parse_text();
let result = parse_loop(configured_loop_count(), &text);
println!("{}", result);
std::process::exit(if result == EXPECTED_CHECKSUM { 0 } else { 1 });
}