A Collection of Code Snippets in as Many Programming Languages as Possible
This project is maintained by TheRenegadeCoder
Welcome to the Even Odd in Rust page! Here, you'll find the source code for this program as well as a description of how the program works.
// Requirement is in https://sample-programs.therenegadecoder.com/projects/even-odd/
// Program to accept an integer on the command line and outputs if the integer is Even or Odd.
use std::env::args;
use std::process::exit;
use std::str::FromStr;
fn usage() -> ! {
println!("Usage: please input a number");
exit(0);
}
fn parse_int<T: FromStr>(s: &str) -> Result<T, <T as FromStr>::Err> {
s.trim().parse::<T>()
}
fn main() {
let mut args = args().skip(1);
// Exit if 1st command-line argument not an integer
let mut input_num: i32 = args
.next()
.and_then(|s| parse_int(&s).ok())
.unwrap_or_else(|| usage());
// Even if divisible by 2, Odd otherwise
match input_num % 2 == 0 {
true => println!("Even"),
false => println!("Odd"),
}
}
Even Odd in Rust was written by:
If you see anything you'd like to change or update, please consider contributing.
No 'How to Implement the Solution' section available. Please consider contributing.
No 'How to Run the Solution' section available. Please consider contributing.