FordoA455
FordoA455

Reputation: 23

Why when I access a HashMap in rust it prints Some in front of the text?

I am trying to make a decimal to hexidecimal converter in rust, and it works fine. However, it prints Some("") in front of the output like Some("1")Some("A")Some("4"). Does anyone know how to fix this? It could be the result of using String::from or I may need to parse the answer. Sorry if this is some easy fix as I am currently learning rust and I do not know all of the intricacies of rust. Thank you in advance! main.rs here:

use std::{
    io::{
        self,
        Write,
    },
};
use std::collections::HashMap;
use std::process;
fn main() {
    let mut hex_number_system = HashMap::new();
    hex_number_system.insert(1,String::from("1"));
    hex_number_system.insert(2,String::from("2"));
    hex_number_system.insert(3,String::from("3"));
    hex_number_system.insert(4,String::from("4"));
    hex_number_system.insert(5,String::from("5"));
    hex_number_system.insert(6,String::from("6"));
    hex_number_system.insert(7,String::from("7"));
    hex_number_system.insert(8,String::from("8"));
    hex_number_system.insert(9,String::from("9"));
    hex_number_system.insert(10,String::from("A"));
    hex_number_system.insert(11,String::from("B"));
    hex_number_system.insert(12,String::from("C"));
    hex_number_system.insert(13,String::from("D"));
    hex_number_system.insert(14,String::from("E"));
    hex_number_system.insert(15,String::from("F"));
    let mut line = 0;
    let mut current_multiplier = 0;
    let mut current_num = String::new();
    let mut digit_num = 256;
    print!("Enter a number from 0 - 4095:");
    io::stdout().flush().unwrap();
    let mut input = String::new();
    io::stdin().read_line(&mut input).unwrap();
    println!("{:?}", input);
    let mut user = input.trim().parse::<i32>().unwrap();
    if user > 4095 {
        println!("Too high");
        process::exit(1);
    }
    if user < 0 {
        println!("Too low");
        process::exit(1);
    }
    for i in 1..=3 {
        current_multiplier = 15;
        loop {
            if current_multiplier == 0 {
                print!("{}", 0);
                digit_num /= 16;
                break;
            }
            if user >= (current_multiplier * digit_num) {
                print!("{:?}", hex_number_system.get(&current_multiplier));
                user -= &digit_num * &current_multiplier;
                digit_num /= 16;
                break;
            } else {
                current_multiplier -= 1;
            }
        }
    }
    print!("\n");
}

Upvotes: 1

Views: 68

Answers (1)

Gnurfos
Gnurfos

Reputation: 1010

If you look at the doc of the HashMap::get function, you'll see that it returns an Option. The doc doesn't say, but that's to handle the case when the key is not found in the map (the example in the doc shows this).

You have to handle this possibility. If you don't mind your code crashing, you can just .unwrap() the result. Otherwise, match it properly.

Upvotes: 1

Related Questions