Hi,
I find the following fn I wrote in rust to convert a string into a decimal number too clumsy working though. Input from stdin is passed to this function to get the decimal number or to throw error if input is not in the range of 10..2.
How to make it better? Mostly, I (from C background to rust) searched through the available methods in std library and chose one, help on idiomatic rust way is sought.
fn string_to_decimal(s: &mut String) -> Result<u8, String> {
let len = s.trim().len();
if len == 0 || len > 2{
Err("Maximum players 10 & minimum players 2".to_string())
} else if len == 2{
let mut num: u8 = 0;
let mut ch = s.remove(0); /* Tens */
if let Some(tens) = ch.to_digit(10) {
num += (tens * 10) as u8;
}
ch = s.remove(0); /* Ones */
if let Some(ones) = ch.to_digit(10){
num += ones as u8;
}
if num <= 10 {
Ok(num)
}else {
Err("Maximum players allowed is 10".to_string())
}
} else /* len is 1*/{
let ch = s.remove(0); /* Ones */
if let Some(ones) = ch.to_digit(10){
if ones < 10 && ones > 1 {
Ok(ones as u8)
} else {
Err("Maximum players 10 & minimum players 2".to_string())
}
}else {
Err("Maximum players 10 & minimum players 2".to_string())
}
}
}