I'm pretty new to Rust, coming from a background in mostly C. After reading the Rust Book and following along in my editor, then doing most of the Rustlings course, I decided the best way to really learn was basically jumping in and writing some programs for myself.
My current program has a function that takes a String as an argument and parses it to a u64 before performing operations on it. Currently my error handling terminates the program if this is unsuccessful.
Code snippet:
let value: u64 = match value.trim().parse() {
Ok(num) => num,
Err(c) => {
eprintln!("Error: {}", c);
process::exit(1);
}
};
This works but is not ideal. Ideally what I want to happen is that the program will continue processing any additional arguments and just record the failure, then exit later with failure. In C, I would create a global variable for the exit value, initialize it to 0 and just change it to 1 upon failure. I have read about the static keyword and it looks like I could implement this basically the same way with a static variable that is changed upon any failure, but I was wondering if there is any better way, as Rust so far seems to be full of interesting ideas that solve a lot of common C pitfalls more gracefully.