Prime Number in Rust

Published on 31 October 2019 (Updated: 31 October 2019)

Welcome to the Prime Number in Rust page! Here, you'll find the source code for this program as well as a description of how the program works.

Current Solution

//  Requirement
// Accept a number on command line and print if it is Composite or Prime 
// Works till  39 digits, ...

use std::env::args;
use std::process::exit;
use std::str::FromStr;

fn usage() -> ! {
    println!("Usage: please input a non-negative integer");

fn parse_int<T: FromStr>(s: &str) -> Result<T, <T as FromStr>::Err> {

fn main() {
    let mut args = args().skip(1);

    // Exit if 1st command-line argument not an positive integer
    let input_num: u128 = args
        .and_then(|s| parse_int(&s).ok())
        .unwrap_or_else(|| usage());

    if input_num < 2 || (input_num != 2 && input_num % 2 == 0) {

    let mut n = 3u128;
    while n * n <= input_num {
        if input_num % n == 0 {
        n += 2;

Prime Number in Rust was written by:

If you see anything you'd like to change or update, please consider contributing.

Note: The solution shown above is the current solution in the Sample Programs repository as of May 08 2023 19:53:07. The solution was first committed on Oct 31 2019 18:11:28. As a result, documentation below may be outdated.

How to Implement the Solution

No 'How to Implement the Solution' section available. Please consider contributing.

How to Run the Solution

No 'How to Run the Solution' section available. Please consider contributing.