0

I'm trying to translate a sales tax Python program to Rust. The program is basically finished, but I'd like to make it so that if the user doesn't enter a valid input (state dollar amount), the program loops until a valid entry is made.

However, if I enter the abbreviation of a state that doesn't exist, the program still runs and returns incorrect amounts before exiting.

In Python, I could do validation with TRY...EXCEPT in a loop.

I have no idea if there is an analogous way to do this in Rust.

use std::collections::HashMap;
use std::io;

fn read_one() -> String {
    let mut words = String::new();
    io::stdin()
        .read_line(&mut words)
        .ok()
        .expect("Failed to read line.");
    words
}

fn main() {
    let mut tax_rates = HashMap::new();
    tax_rates.insert("AL".to_string(), 0.04);
    tax_rates.insert("AK".to_string(), 0.00);
    tax_rates.insert("AZ".to_string(), 0.056);
    tax_rates.insert("AR".to_string(), 0.65);
    tax_rates.insert("CA".to_string(), 0.725);
    tax_rates.insert("CO".to_string(), 0.029);
    tax_rates.insert("CT".to_string(), 0.635);
    tax_rates.insert("DE".to_string(), 0.00);
    tax_rates.insert("DC".to_string(), 0.06);
    tax_rates.insert("FL".to_string(), 0.06);
    tax_rates.insert("GA".to_string(), 0.04);
    tax_rates.insert("HI".to_string(), 0.04);
    tax_rates.insert("ID".to_string(), 0.06);
    tax_rates.insert("IL".to_string(), 0.0625);
    tax_rates.insert("IN".to_string(), 0.07);
    tax_rates.insert("IA".to_string(), 0.06);
    tax_rates.insert("KS".to_string(), 0.065);
    tax_rates.insert("KY".to_string(), 0.06);
    tax_rates.insert("LA".to_string(), 0.0445);
    tax_rates.insert("ME".to_string(), 0.055);
    tax_rates.insert("MD".to_string(), 0.06);
    tax_rates.insert("MA".to_string(), 0.0625);
    tax_rates.insert("MI".to_string(), 0.06);
    tax_rates.insert("MN".to_string(), 0.06875);
    tax_rates.insert("MS".to_string(), 0.07);
    tax_rates.insert("MO".to_string(), 0.0425);
    tax_rates.insert("MT".to_string(), 0.00);
    tax_rates.insert("NE".to_string(), 0.055);
    tax_rates.insert("NV".to_string(), 0.0685);
    tax_rates.insert("NH".to_string(), 0.00);
    tax_rates.insert("NJ".to_string(), 0.06625);
    tax_rates.insert("NM".to_string(), 0.05125);
    tax_rates.insert("NY".to_string(), 0.04);
    tax_rates.insert("NC".to_string(), 0.0475);
    tax_rates.insert("ND".to_string(), 0.05);
    tax_rates.insert("OH".to_string(), 0.0575);
    tax_rates.insert("OK".to_string(), 0.045);
    tax_rates.insert("OR".to_string(), 0.00);
    tax_rates.insert("PA".to_string(), 0.06);
    tax_rates.insert("RI".to_string(), 0.07);
    tax_rates.insert("PR".to_string(), 0.115);
    tax_rates.insert("SC".to_string(), 0.06);
    tax_rates.insert("SD".to_string(), 0.045);
    tax_rates.insert("TN".to_string(), 0.07);
    tax_rates.insert("TX".to_string(), 0.0625);
    tax_rates.insert("UT".to_string(), 0.0485);
    tax_rates.insert("VA".to_string(), 0.043);
    tax_rates.insert("VT".to_string(), 0.06);
    tax_rates.insert("WA".to_string(), 0.065);
    tax_rates.insert("WV".to_string(), 0.06);
    tax_rates.insert("WI".to_string(), 0.05);
    tax_rates.insert("WY".to_string(), 0.04);

    println!("In what state are you making your purchase?");
    let state = read_one();
    let state = state.trim().to_uppercase();

    println!("How many items are you purchasing?");
    let purchase_number = read_one();
    let purchase_number = purchase_number.trim().parse::<i32>().unwrap();

    match tax_rates.get(&state) {
        Some(rate) => {
            println!("The tax rate for {} is ({}).", state, rate);
        }
        None => {
            println!("None.");
        }
    }

    let mut tax_vec = Vec::new();
    let mut cost_vec = Vec::new();

    for item_number in 0..purchase_number {
        println!("What is the cost of item number {}?", item_number + 1);
        let item_cost = read_one();
        let item_cost = item_cost.trim().parse::<f64>().unwrap();

        match tax_rates.get(&state) {
            Some(rate) => {
                let tax_amount = item_cost * rate;
                let fin_cost = item_cost + tax_amount;
                println!(
                    "The tax amount for item {} is: {:.2}",
                    item_number, tax_amount
                );
                println!(
                    "The final cost of item number {} is: {:.2}",
                    item_number, fin_cost
                );

                tax_vec.push(tax_amount);
                cost_vec.push(fin_cost);
            }

            None => {
                println!("None.");
            }
        }
    }

    let mut t_total = 0.00;
    let mut c_total = 0.00;

    for i in tax_vec {
        t_total += i
    }

    for b in cost_vec {
        c_total += b
    }
    println!("Your total tax amount is {:.2}", t_total);
    println!("Yout total cost is {:.2}", c_total);
}

How can I validate user input in Rust?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
muffledcry
  • 129
  • 1
  • 5
  • It's hard to answer your question because it doesn't include a [MRE]. Please remove everything that isn't needed to reproduce the problem. There are [Rust-specific MRE tips](//stackoverflow.com/tags/rust/info) you can use to reduce your original code for posting here. Thanks! – Shepmaster Jan 03 '20 at 20:41
  • *if I enter the abbreviation of a state that doesn't exist* — you have explicitly written code that handles this case (`match tax_rates.get(&state) ... None =>`). Why prevents you from putting that inside a loop, as you want? – Shepmaster Jan 03 '20 at 20:42
  • See also [How do I create a map from a list in a functional way?](https://stackoverflow.com/q/30441698/155423) to shorten some of your code. – Shepmaster Jan 03 '20 at 20:43

1 Answers1

3

I suggest looping repeatedly until your program knows it has valid input.

Try replacing this code:

println!("In what state are you making your purchase?");
let state = read_one();
let state = state.trim().to_uppercase();

with this code:

let mut state: String;
loop {
    println!("In which state are you making your purchase?");
    state = read_one().trim().to_uppercase();

    if tax_rates.contains_key(&state) {
        break
    }

    println!("Invalid state.  Please try again.")
}

This only handles the state input, so you'll have to do something similar with the other input variables.

J-L
  • 1,786
  • 10
  • 13