Roman Numeral in Rust

Published on 28 April 2022 (Updated: 25 September 2022)

Roman Numeral in Rust

Welcome to the Roman Numeral 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

use std::env;

fn main() {
    let args: Vec<String> = env::args().collect();

    if args.len() < 2 {
        println!("Usage: {} ROMAN NUMERAL", args[0]);
    } else {
        println!("{}", convert_roman_numeral(&args[1]));
    }
}

fn convert_char(c: char) -> u64 {
    if c == 'I' {
        return 1;
    } else if c == 'V' {
        return 5;
    } else if c == 'X' {
        return 10;
    } else if c == 'L' {
        return 50;
    } else if c == 'C' {
        return 100;
    } else if c == 'D' {
        return 500;
    } else if c == 'M' {
        return 1000;
    } else {
        return 0;
    }
}

fn convert_roman_numeral(s: &String) -> u64 {
    let mut number = 0u64;
    for c in s.as_bytes() {
        number += convert_char(*c as char)
    }

    number
}

Roman Numeral in Rust was written by:

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

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.