One of the most loved features of Rust is the way it lets you handle errors. The Result
type is a powerful tool that allows you to handle errors in a way that is both safe and expressive. In this challenge, you will be working with the Result<T, E>
type to handle errors in a graceful way.
The Result<T, E>
itself is an enum that has two variants: Ok(T)
and Err(E)
. The Ok
variant is used to represent a successful computation that returns a value of type T
. The Err
variant is used to represent an error that returns a value of type E
.
When you have a function that can fail, you can use the Result
type to return the result of the computation. If the computation is successful, you can return the success variant of Result
with the value of the computation. If the computation fails, you can return the error variant of Result
with an error message that explains what went wrong.
In this challenge, you're given a function, parse_percentage
that takes a string as input and returns a Result
type. The function should parse the input string as a percentage and return the percentage as a u8
if the input is valid. If the input is invalid, the function should return an error message as a String
.
Parsing from a string to a number can fail for many reasons. For example, the input string may not be a valid number, or it may be a valid number but not a valid percentage. Your task is to handle these errors gracefully and return an error message that explains what went wrong.
Complete the function, if the parsing was successful return a success variant of the Result
, if there was an error in parsing, return an error variant of the Result
with an error message.
Result
with the percentage as a Ok(u8)
.String
Err("Percentage out of range")
.String
Err("Invalid input")
.pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here // Attempt to parse value from input let res: Result<u8, _> = input.parse(); match res { // If parse successful and v in acceptable range, return v Ok(v) if v <= 100 => Ok(v), // If parse successful but outside of acceptable range Ok(_) => Err("Percentage out of range".to_string()), // Parse unsuccessful Err(_) => Err("Invalid input".to_string()), }}// Example usagepub fn main() { let result = parse_percentage("50"); assert_eq!(result, Ok(50)); let result = parse_percentage("101"); assert_eq!(result, Err("Percentage out of range".to_string())); let result = parse_percentage("abc"); assert_eq!(result, Err("Invalid input".to_string()));}
pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here if let Ok(v) = input.parse::<u8>() { if v <= 100 { Ok(v) } else { Err("Percentage out of range".to_string()) } } else { Err("Invalid input".to_string()) }}// Example usagepub fn main() { let result = parse_percentage("50"); assert_eq!(result, Ok(50)); let result = parse_percentage("101"); assert_eq!(result, Err("Percentage out of range".to_string())); let result = parse_percentage("abc"); assert_eq!(result, Err("Invalid input".to_string()));}
pub fn parse_percentage(input: &str) -> Result<u8, String> { match input.parse::<u8>() { Ok(v) if (0..=100).contains(&v) => Ok(v), Ok(_) => Err("Percentage out of range".to_string()), Err(_) => Err("Invalid input".to_string()), }}// Example usagepub fn main() { let result = parse_percentage("50"); assert_eq!(result, Ok(50)); let result = parse_percentage("101"); assert_eq!(result, Err("Percentage out of range".to_string())); let result = parse_percentage("abc"); assert_eq!(result, Err("Invalid input".to_string()));}
pub fn parse_percentage(input: &str) -> Result<u8, String> { let p = input.parse::<i32>(); return match p { Ok(v) => { if v >= 0 && v <= 100 { return Ok(v as u8) } else { return Err("Percentage out of range".to_string()); } } Err(_) => Err("Invalid input".to_string()) }}// Example usagepub fn main() { let result = parse_percentage("50"); assert_eq!(result, Ok(50)); let result = parse_percentage("101"); assert_eq!(result, Err("Percentage out of range".to_string())); let result = parse_percentage("abc"); assert_eq!(result, Err("Invalid input".to_string()));}
pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here let percentage: Result<u8, _> = input.parse(); match percentage { Err(_) => Err("Invalid input".to_string()), Ok(number) => { if (0..=100).contains(&number) { Ok(number) } else { Err("Percentage out of range".to_string()) } } }}// Example usagepub fn main() { let result = parse_percentage("50"); assert_eq!(result, Ok(50)); let result = parse_percentage("101"); assert_eq!(result, Err("Percentage out of range".to_string())); let result = parse_percentage("abc"); assert_eq!(result, Err("Invalid input".to_string()));}
pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here //let guess: u8 = input.trim().parse().ok(); let input_number: Option<u8> = input.trim().parse().ok(); match input_number{ Some(num) => { if num <= 100 { return Ok(num) } else { return Err("Percentage out of range".to_string()) } }, None => { return Err("Invalid input".to_string()) } };}// Example usagepub fn main() { let result = parse_percentage("50"); assert_eq!(result, Ok(50)); let result = parse_percentage("101"); assert_eq!(result, Err("Percentage out of range".to_string())); let result = parse_percentage("abc"); assert_eq!(result, Err("Invalid input".to_string()));}
pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here match input.parse::<u8>() { Ok(num) if num <= 100 => Ok(num), Ok(_) => Err("Percentage out of range".to_string()), Err(_) => Err("Invalid input".to_string()) }}// Example usagepub fn main() { let result = parse_percentage("50"); assert_eq!(result, Ok(50)); let result = parse_percentage("101"); assert_eq!(result, Err("Percentage out of range".to_string())); let result = parse_percentage("abc"); assert_eq!(result, Err("Invalid input".to_string()));}
pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function her match input.parse::<u8>() { Ok(num) if num <= 100 => Ok(num), Ok(_) => Err("Percentage out of range".to_string()), Err(_) => Err("Invalid input".to_string()), }}// Example usagepub fn main() { let result = parse_percentage("50"); assert_eq!(result, Ok(50)); let result = parse_percentage("101"); assert_eq!(result, Err("Percentage out of range".to_string())); let result = parse_percentage("abc"); assert_eq!(result, Err("Invalid input".to_string()));}
pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here let num:i32=input.parse().map_err(|_| "Invalid input")?; if num>100 || num<0{ return Err("Percentage out of range".to_string()); } return Ok(num as u8);}// Example usagepub fn main() { let result = parse_percentage("50"); assert_eq!(result, Ok(50)); let result = parse_percentage("101"); assert_eq!(result, Err("Percentage out of range".to_string())); let result = parse_percentage("abc"); assert_eq!(result, Err("Invalid input".to_string()));}
pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here match input.parse::<u8>() { Ok(number) if number > 100 => Err(String::from("Percentage out of range")), Ok(number) => Ok(number), Err(_) => Err(String::from("Invalid input")) }}// Example usagepub fn main() { let result = parse_percentage("50"); assert_eq!(result, Ok(50)); let result = parse_percentage("101"); assert_eq!(result, Err("Percentage out of range".to_string())); let result = parse_percentage("abc"); assert_eq!(result, Err("Invalid input".to_string()));}
pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here match input.parse::<u8>() { Ok(number) if number > 100 => Err(String::from("Percentage out of range")), Ok(number) => Ok(number), Err(_) => Err(String::from("Invalid input")) }}// Example usagepub fn main() { let result = parse_percentage("50"); assert_eq!(result, Ok(50)); let result = parse_percentage("101"); assert_eq!(result, Err("Percentage out of range".to_string())); let result = parse_percentage("abc"); assert_eq!(result, Err("Invalid input".to_string()));}
pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here let number: u8 = input.parse().map_err(|_|"Invalid input".to_string())?; if number > 100 { return Err("Percentage out of range".to_string()); } Ok(number) }// Example usagepub fn main() { let result = parse_percentage("50"); assert_eq!(result, Ok(50)); let result = parse_percentage("101"); assert_eq!(result, Err("Percentage out of range".to_string())); let result = parse_percentage("abc"); assert_eq!(result, Err("Invalid input".to_string()));}
pub fn parse_percentage(input: &str) -> Result<u8, String> { if let Ok(number) = input.parse::<u8>() { if number <=100 { return Ok(number); } else { return Err("Percentage out of range".to_string()); } } else { return Err("Invalid input".to_string()); }}// Example usagepub fn main() { let result = parse_percentage("50"); assert_eq!(result, Ok(50)); let result = parse_percentage("101"); assert_eq!(result, Err("Percentage out of range".to_string())); let result = parse_percentage("abc"); assert_eq!(result, Err("Invalid input".to_string()));}
pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here let number: u8 = input.parse().map_err(|_| "Invalid input".to_string())?; if number > 100 { return Err("Percentage out of range".to_string()); } Ok(number)}// Example usagepub fn main() { let result = parse_percentage("50"); assert_eq!(result, Ok(50)); let result = parse_percentage("101"); assert_eq!(result, Err("Percentage out of range".to_string())); let result = parse_percentage("abc"); assert_eq!(result, Err("Invalid input".to_string()));}
pub fn parse_percentage(input: &str) -> Result<u8, String> { match input.parse() { Ok(p @ 0..=100) => Ok(p), Ok(_) => Err("Percentage out of range".into()), Err(_) => Err("Invalid input".into()), }}// Example usagepub fn main() { let result = parse_percentage("50"); assert_eq!(result, Ok(50)); let result = parse_percentage("101"); assert_eq!(result, Err("Percentage out of range".to_string())); let result = parse_percentage("abc"); assert_eq!(result, Err("Invalid input".to_string()));}
pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here match input.parse::<u8>() { Ok(i) if i <= 100 => Ok(i), Ok(i) if i > 100 => Err("Percentage out of range".to_string()), _ => Err("Invalid input".to_string()) }}// Example usagepub fn main() { let result = parse_percentage("50"); assert_eq!(result, Ok(50)); let result = parse_percentage("101"); assert_eq!(result, Err("Percentage out of range".to_string())); let result = parse_percentage("abc"); assert_eq!(result, Err("Invalid input".to_string()));}
pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here let parsed = input.parse::<i32>(); if let Ok(percentage) = parsed { if !(0..=100).contains(&percentage) { return Err(String::from("Percentage out of range")); } }; parsed.map(|percentage| percentage as u8).map_err(|_| String::from("Invalid input"))}// Example usagepub fn main() { let result = parse_percentage("50"); assert_eq!(result, Ok(50)); let result = parse_percentage("101"); assert_eq!(result, Err("Percentage out of range".to_string())); let result = parse_percentage("abc"); assert_eq!(result, Err("Invalid input".to_string()));}
pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here if let Ok(value) = input.parse::<u8>() { if 0 <= value && value <= 100 { Ok(value) } else { Err("Percentage out of range".into()) } } else { Err("Invalid input".into()) }}// Example usagepub fn main() { let result = parse_percentage("50"); assert_eq!(result, Ok(50)); let result = parse_percentage("101"); assert_eq!(result, Err("Percentage out of range".to_string())); let result = parse_percentage("abc"); assert_eq!(result, Err("Invalid input".to_string()));}
pub fn parse_percentage(input: &str) -> Result<u8, String> { match input.parse::<u8>() { Ok(value @ 0..=100) => Ok(value), Ok(_) => Err("Percentage out of range".to_string()), Err(_) => Err("Invalid input".to_string()), }}// Example usagepub fn main() { let result = parse_percentage("50"); assert_eq!(result, Ok(50)); let result = parse_percentage("101"); assert_eq!(result, Err("Percentage out of range".to_string())); let result = parse_percentage("abc"); assert_eq!(result, Err("Invalid input".to_string()));}
pub fn parse_percentage(input: &str) -> Result<u8, String> { match input.parse::<u8>() { Ok(pct) => { if pct > 100 { Err("Percentage out of range".to_string()) } else { Ok(pct) } } Err(_) => Err("Invalid input".to_string()), }}// Example usagepub fn main() { let result = parse_percentage("50"); assert_eq!(result, Ok(50)); let result = parse_percentage("101"); assert_eq!(result, Err("Percentage out of range".to_string())); let result = parse_percentage("abc"); assert_eq!(result, Err("Invalid input".to_string()));}
pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here match input.parse::<i32>() { Ok(percent) => { match percent { -100..=100 => Ok(percent as u8), _ => Err("Percentage out of range".to_string()), } } Err(_) => Err("Invalid input".to_string()), }}// Example usagepub fn main() { let result = parse_percentage("50"); assert_eq!(result, Ok(50)); let result = parse_percentage("101"); assert_eq!(result, Err("Percentage out of range".to_string())); let result = parse_percentage("abc"); assert_eq!(result, Err("Invalid input".to_string()));}
pub fn parse_percentage(input: &str) -> Result<u8, String> { match str::parse::<u8>(input) { Ok(percentage) => { if percentage < 0 || percentage > 100 { return Err("Percentage out of range".to_string()); } return Ok(percentage); } Err(_e) => { return Err("Invalid input".to_string()); } }}// Example usagepub fn main() { let result = parse_percentage("50"); assert_eq!(result, Ok(50)); let result = parse_percentage("101"); assert_eq!(result, Err("Percentage out of range".to_string())); let result = parse_percentage("abc"); assert_eq!(result, Err("Invalid input".to_string()));}
pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here match input.parse::<u8>() { Ok(value) if value <= 100 => Ok(value), Ok(_) => Err(String::from("Percentage out of range")), Err(_) => Err(String::from("Invalid input")), }}// Example usagepub fn main() { let result = parse_percentage("50"); assert_eq!(result, Ok(50)); let result = parse_percentage("101"); assert_eq!(result, Err("Percentage out of range".to_string())); let result = parse_percentage("abc"); assert_eq!(result, Err("Invalid input".to_string()));}
pub fn parse_percentage(input: &str) -> Result<u8, String> { match input.parse::<u8>() { Ok(value) if value <= 100 => Ok(value), Ok(_) => Err(String::from("Percentage out of range")), Err(_) => Err(String::from("Invalid input")), }}// Example usagepub fn main() { let result = parse_percentage("50"); assert_eq!(result, Ok(50)); let result = parse_percentage("101"); assert_eq!(result, Err("Percentage out of range".to_string())); let result = parse_percentage("abc"); assert_eq!(result, Err("Invalid input".to_string()));}
pub fn parse_percentage(input: &str) -> Result<u8, String> { let percent = match input.parse::<u8>() { Ok(percent) => percent, Err(_) => return Err(format!("Invalid input")), }; if percent > 100 { return Err("Percentage out of range".to_string()); } Ok(percent)}// Example usagepub fn main() { let result = parse_percentage("50"); assert_eq!(result, Ok(50)); let result = parse_percentage("101"); assert_eq!(result, Err("Percentage out of range".to_string())); let result = parse_percentage("abc"); assert_eq!(result, Err("Invalid input".to_string()));}
pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here let percentage: u8 = input.parse().map_err(|_| "Invalid input")?; if percentage > 100 { return Err("Percentage out of range".to_string()); } Ok(percentage)}// Example usagepub fn main() { let result = parse_percentage("50"); assert_eq!(result, Ok(50)); let result = parse_percentage("101"); assert_eq!(result, Err("Percentage out of range".to_string())); let result = parse_percentage("abc"); assert_eq!(result, Err("Invalid input".to_string()));}
pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here let percentage: u8 = input.parse().map_err(|_| "Invalid input")?; if percentage > 100 { return Err("Percentage out of range".to_string()); } Ok(percentage)}// Example usagepub fn main() { let result = parse_percentage("50"); assert_eq!(result, Ok(50)); let result = parse_percentage("101"); assert_eq!(result, Err("Percentage out of range".to_string())); let result = parse_percentage("abc"); assert_eq!(result, Err("Invalid input".to_string()));}
pub fn parse_percentage(input: &str) -> Result<u8, String> { if input.parse::<u8>().is_ok() { let number = input.parse::<u8>().unwrap(); //don't need to include `if number > 0` as u8 means the number is always positive if number > 100 { return Err("Percentage out of range".to_string()); } return Ok(number); } Err("Invalid input".to_string())}// Example usagepub fn main() { let result = parse_percentage("50"); assert_eq!(result, Ok(50)); let result = parse_percentage("101"); assert_eq!(result, Err("Percentage out of range".to_string())); let result = parse_percentage("abc"); assert_eq!(result, Err("Invalid input".to_string()));}
pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here let percentage = input.parse::<u8>().map_err(|_| "Invalid input".to_string())?; if percentage > 100 { return Err("Percentage out of range".to_string()) } Ok(percentage)}// Example usagepub fn main() { let result = parse_percentage("50"); assert_eq!(result, Ok(50)); let result = parse_percentage("101"); assert_eq!(result, Err("Percentage out of range".to_string())); let result = parse_percentage("abc"); assert_eq!(result, Err("Invalid input".to_string()));}
pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here match input.parse::<u8>() { Ok(x) => { if x > 100 { return Err("Percentage out of range".to_string()); } Ok(x) }, Err(_e) => Err("Invalid input".to_string()), }}
pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here match input.parse::<u8>() { Ok(x) => { if x > 100 || x < 0 { return Err("Percentage out of range".to_string()); } Ok(x) }, Err(_e) => Err("Invalid input".to_string()), }}
pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here match input.parse::<u8>() { Ok(i) => { if (0..=100).contains(&i) { Ok(i) } else { Err(String::from("Percentage out of range")) } }, Err(_) => Err(String::from("Invalid input")) }}// Example usagepub fn main() { let result = parse_percentage("50"); assert_eq!(result, Ok(50)); let result = parse_percentage("101"); assert_eq!(result, Err("Percentage out of range".to_string())); let result = parse_percentage("abc"); assert_eq!(result, Err("Invalid input".to_string()));}
pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here if let Ok(p) = input.parse::<u8>() { if p <= 100 { Ok(p) } else { Err("Percentage out of range".to_string()) } } else { Err("Invalid input".to_string()) }}// Example usagepub fn main() { let result = parse_percentage("50"); assert_eq!(result, Ok(50)); let result = parse_percentage("101"); assert_eq!(result, Err("Percentage out of range".to_string())); let result = parse_percentage("abc"); assert_eq!(result, Err("Invalid input".to_string()));}
pub fn parse_percentage(input: &str) -> Result<u8, String> { let parsed_int_result = input.parse::<u8>(); if let Ok(parsed_int) = parsed_int_result { if parsed_int <= 100 { Ok(parsed_int) } else { Err("Percentage out of range".to_string()) } } else { Err("Invalid input".to_string()) }}// Example usagepub fn main() { let result = parse_percentage("50"); assert_eq!(result, Ok(50)); let result = parse_percentage("101"); assert_eq!(result, Err("Percentage out of range".to_string())); let result = parse_percentage("abc"); assert_eq!(result, Err("Invalid input".to_string()));}
pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here let percentage = input.parse::<u8>().map_err(|e| "Invalid input".to_string())?; if percentage > 100 { Err(String::from("Percentage out of range")) } else { Ok(percentage) }}// Example usagepub fn main() { let result = parse_percentage("50"); assert_eq!(result, Ok(50)); let result = parse_percentage("101"); assert_eq!(result, Err("Percentage out of range".to_string())); let result = parse_percentage("abc"); assert_eq!(result, Err("Invalid input".to_string()));}
pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here match input.parse() { Ok(value) => match value { 0..=100 => Ok(value), _ => Err("Percentage out of range".to_string()) }, Err(_) => Err("Invalid input".to_string()), }}
pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here match input.parse() { Ok(n) => { if n > 100 { Err("Percentage out of range".to_string()) } else { Ok(n) } }, Err(_) => Err("Invalid input".to_string()) }}
pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here if input.chars().all(|c| c.is_alphabetic()) { return Err("Invalid input".to_string()); } if input.parse::<i32>().unwrap() > 100 || input.parse::<i32>().unwrap() < 0 { return Err("Percentage out of range".to_string()); } Ok(input.parse().unwrap())}
pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here match input.parse::<u8>() { Ok(0 ..= 100) => Ok(input.parse::<u8>().unwrap()), Ok(_i) => Err("Percentage out of range".to_string()), _ => Err("Invalid input".to_string()), }}
pub fn parse_percentage(input: &str) -> Result<u8, String> { match input.parse() { Ok(val) => match val { 0..=100 => Ok(val), _ => Err("Percentage out of range".to_string()), } Err(_) => Err("Invalid input".to_string()), }}
pub fn parse_percentage(input: &str) -> Result<u8, String> { match input.parse() { Ok (val) => match val { 0..=100 => Ok(val), _ => Err("Percentage out of range".to_string()), }, Err(_) => Err("Invalid input".to_string()), }}
pub fn parse_percentage(input: &str) -> Result<u8, String> { match input.parse() { Ok(num) => match num { 0..=100 => Ok(num), _ => Err("Percentage out of range".to_string()), }, Err(_) => Err("Invalid input".to_string()), }}
pub fn parse_percentage(input: &str) -> Result<u8, String> { let parsed = match input.parse::<u8>() { Err(_) => { return Err("Invalid input".to_string());} Ok(i) => i }; if parsed <= 100 {return Ok(parsed);} else {return Err("Percentage out of range".to_string());}}
pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here match input.parse::<i32>() { Ok(value) => { if value < 0 || value > 100 { Err("Percentage out of range".to_string()) } else { Ok(value as u8) } }, Err(_) => Err("Invalid input".to_string()) }}
pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here match input.parse::<u8>(){ Ok(num) => match num{ 0..=100 => Ok(num), _ => Err("Percentage out of range".to_string()) } Err(_) => Err("Invalid input".to_string()) }}
pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here match input.parse::<u8>() { Ok(n) => if n <= 100 { Ok(n) } else { Err("Percentage out of range".to_string()) }, _ => Err("Invalid input".to_string()) }}
pub fn parse_percentage(input: &str) -> Result<u8, String> { let i: Result<u8, _> = input.parse(); match i { Ok(i) => { if i <= 100 { Ok(i) } else { Err("Percentage out of range".to_string()) } } Err(_) => Err("Invalid input".to_string()) }}
pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here let number: Result<u8, _> = input.parse(); match number { Ok(number) => { if number <= 100 { return Ok(number); } else { return Err("Percentage out of range".to_string()); } }, Err(_) => { return Err("Invalid input".to_string()); } }}
pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here let parse_res = input.parse::<i16>(); match parse_res { Err(_) => Err("Invalid input".to_string()), Ok(n) => match n { 0..=100 => Ok(n as u8), _ => Err("Percentage out of range".to_string()) } }}
pub fn parse_percentage(input: &str) -> Result<u8, String> { if input.chars().nth(0).unwrap() < '0' || input.chars().nth(0).unwrap() > '9' { return Err("Invalid input".to_string()) } let perc = input.parse::<u8>().unwrap(); if perc > 100 { Err("Percentage out of range".to_string()) } else { Ok(perc) }}