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(input: &str) -> Result<u8, String>
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 u8
.String
"Percentage out of range".String
"Invalid input".and1
pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here match input.parse::<u8>() { Ok(number) if number <= 100 => Ok(number), Ok(_) => Err("Percentage out of range".to_string()), Err(_) => Err("Invalid input".to_string()) }}
michdorf
pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here let num = match input.parse::<u8>() { Ok(num) => num, Err(_) => return Err(String::from("Invalid input")) }; return match num { 0..=100 => Ok(num), _ => Err(String::from("Percentage out of range")) }}
javigar322
pub fn parse_percentage(input: &str) -> Result<u8, String> { if let Ok(num) = input.parse::<u8>() { if num > 100 { return Err("Percentage out of range".to_string()); } else { return Ok(num); } } return Err("Invalid input".to_string());}
GiulianoCTRL
pub fn parse_percentage(input: &str) -> Result<u8, String> { if let Ok(num) = input.parse::<u8>() { if num > 100 { return Err("Percentage out of range".to_string()); } return Ok(num); } return Err("Invalid input".to_string());}
mgreve
pub fn parse_percentage(input: &str) -> Result<u8, String> { let mut n:u8 = 0; for c in input.chars() { println!("{}", c); if '0' <= c && c <= '9' { n = n*10 + c.to_digit(10).unwrap() as u8; } else { return Result::Err("Invalid input".into()); } if n > 100 { return Result::Err("Percentage out of range".into()); } } Result::Ok(n)}
kobapi28
pub fn parse_percentage(input: &str) -> Result<u8, String> { match input.parse::<u8>() { Ok(x) if x <= 100 => Ok(x), Err(..) => Err("Invalid input".to_string()), _ => Err("Percentage out of range".to_string()) }}
LenRemmerswaal
pub fn parse_percentage(input: &str) -> Result<u8, String> { match input.parse() { Ok(n) if n <= 100 => Ok(n), Ok(_) => Err("Percentage out of range".to_string()), Err(_) => Err("Invalid input".to_string()) }}
LenRemmerswaal
pub fn parse_percentage(input: &str) -> Result<u8, String> { match input.parse() { Ok(n) if n <= 100 => Ok(n), Ok(_) => Err("Percentage out of range".to_string()), Err(_) => Err("Invalid input".to_string()) }}
galenseilis
pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here let num = input.to_string().parse::<u8>(); match num { Ok(num) => { if num < 0 || num > 100 { return Err("Percentage out of range".to_string()) } else { return Ok(num) } }, Err(num) => Err("Invalid input".to_string()) }}
Kobeieii
pub fn parse_percentage(input: &str) -> Result<u8, String> { match input.parse::<u8>() { Ok(x) if x <= 100 => Ok(x), Err(..) => Err("Invalid input".to_string()), _ => Err("Percentage out of range".to_string()) }}
xenonminer
pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here match input.parse::<u8>() { Ok(percentage) => { if percentage > 100 { return Err("Percentage out of range".into()); }; return Ok(percentage) }, Err(_) => Err("Invalid input".into()) }}
kennethlove
pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here match input.parse::<u8>() { Ok(percentage) => { if percentage > 100 { return Err("Percentage out of range".into()) }; return Ok(percentage) }, Err(_) => Err("Invalid input".into()) }}
tobiaszfilar
pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here match input.parse::<u8>() { Ok(n) => { if n <= 100 { return Ok(n) } return Err("Percentage out of range".to_string()) } Err(_) => Err("Invalid input".to_string()) }}
kylenoteboom
use std::str::FromStr;pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here let number = u8::from_str(input).map_err(|_| "Invalid input")?; if number > 100 { return Err("Percentage out of range".to_string()); } Ok(number)}
5822791760
pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here let res = input.parse::<u8>(); match res { Ok(n) => { if n <= 100 { return Ok(n) } return Err("Percentage out of range".to_string()) } Err(_) => Err("Invalid input".to_string()) }}
arilet
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), Err(_) => Err("Invalid input".to_string()), _ => Err("Percentage out of range".to_string()), }}
martin-unit
pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here match input.parse::<u8>() { Ok(n) if n < 101 => Ok(n), Ok(_) => Err("Percentage out of range".to_string()), Err(_) => Err("Invalid input".to_string()) }}
jimlawton
pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here match input.parse::<i32>() { Ok(n) => { if n < 0 || n > 100 { Err(String::from("Percentage out of range")) } else { Ok(n as u8) } }, Err(_) => Err(String::from("Invalid input")), }}
XtebanUy
pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here match input.parse::<u8>() { Err(_) => Err("Invalid input".to_string()), Ok (n @ 0..=100) => Ok(n), _ => Err("Percentage out of range".to_string()) }}
mbergkvist
pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here let value = input.parse::<u8>().map_err(|_| "Invalid input")?; if value > 100 { Err("Percentage out of range")?; }; Ok(value)}
Kobeieii
pub fn parse_percentage(input: &str) -> Result<u8, String> { match input.parse::<u8>() { Ok(x) if x <= 100 => Ok(x), Err(..) => Err("Invalid input".to_string()), _ => Err("Percentage out of range".to_string()) }}
DivineGod
pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here let num: u32 = input.parse::<u32>().map_err(|_| "Invalid input".to_string())?; match num { n @ (0..=100) => Ok(n as u8), _ => Err("Percentage out of range".to_string()), }}
StimhackSoftware
pub fn parse_percentage(input: &str) -> Result<u8, String> { let out_num = input.parse::<u8>(); match out_num { Ok(x) => { if 0 <= x && x <= 100 { Ok(x) } else { Err("Percentage out of range".into()) } }, Err(e) => Err("Invalid input".into()) }}
Algorab
pub fn parse_percentage(input: &str) -> Result<u8, String> { let num = input.parse::<u8>().map_err(|_| "Invalid input".to_string())?; match num { _ if num > 100 => { Err("Percentage out of range".to_string()) } _ => Ok(num as u8), }}
sroas
pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here match input.parse::<u8>() { Ok(x) => match x { x if x <= 100 => Ok(x), _ => Err("Percentage out of range".to_string()), }, Err(_) => Err("Invalid input".to_string()) }}
xiuchiliudu
pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here let result = input.parse::<i32>(); match result { Ok(num) => { if num > 100 || num < 0 { return Err("Percentage out of range".to_string()); } Ok(num as u8) } Err(_) => Err("Invalid input".to_string()), }}
qiyuan711
pub fn parse_percentage(input: &str) -> Result<u8, String> { match input.parse::<u8>() { Ok(value) => { if value <= 100 { Ok(value) } else { Err("Percentage out of range".to_string()) } }, Err(_) => Err("Invalid input".to_string()) }}
yoakemae
pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here match input.parse::<u8>() { Ok(num) => { if num <= 100 { return Ok(num); } else { return Err("Percentage out of range".to_string()); } }, Err(_) => return Err("Invalid input".to_string()), };}
jw
pub fn parse_percentage(input: &str) -> Result<u8, String> { match input.parse::<u8>() { Ok(num) if num <= 100 => Ok(num as u8), Ok(_) => Err("Percentage out of range".to_string()), Err(_) => Err("Invalid input".to_string()), }}
nt2311-vn
pub fn parse_percentage(input: &str) -> Result<u8, String> { match input.parse::<u8>() { Ok(number) => { if number <= 100 { Ok(number) } else { Err(String::from("Percentage out of range")) } }, Err(_) => Err(String::from("Invalid input"))}}
tinthid
pub fn parse_percentage(input: &str) -> Result<u8, String> { match input.parse::<i32>() { Ok(num) if num >= 0 && num <= 100 => Ok(num as u8), Ok(_) => Err("Percentage out of range".to_string()), Err(_) => Err("Invalid input".to_string()) }}
devarajang
pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here let percent: u8; match input.parse::<u8>() { Ok(n) => percent = n, Err(_) => return Err("Invalid input".to_string()) }; if percent > 100 { return Err("Percentage out of range".to_string()); } return Ok(percent);}
joeslow
pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here let parsed = input.parse::<u8>(); match parsed { Ok(res) => { if res > 100 { Err("Percentage out of range".to_string()) } else { Ok(res) } } Err(_) => Err("Invalid input".to_string()), }}
DV-13
pub fn parse_percentage(input: &str) -> Result<u8, String> { match input.parse::<u8>() { Ok(val) => { if val <= 100 { Ok(val) } else { Err("Percentage out of range".to_string()) } }, Err(_) => return Err("Invalid input".to_string()), }}
DV-13
pub fn parse_percentage(input: &str) -> Result<u8, String> { let res = match input.parse::<u8>() { Ok(val) => val, Err(_) => return Err("Invalid input".to_string()), }; if res > 100 { return Err("Percentage out of range".to_string()); } Ok(res)}
Thymelizabeth
pub fn parse_percentage(input: &str) -> Result<u8, String> { let n = input.parse().map_err(|_| "Invalid input".to_string())?; if n > 100 { Err("Percentage out of range".into()) } else { Ok(n) }}
oDqnger
pub fn parse_percentage(input: &str) -> Result<u8, String> { match input.parse::<u8>() { Ok(val) => { if val >= 0 && val <= 100 { Ok(val) } else { Err("Percentage out of range".to_string()) } }, Err(_) => Err("Invalid input".to_string()) }}
oDqnger
pub fn parse_percentage(input: &str) -> Result<u8, String> { match input.parse::<u8>() { Ok(val) => { if val >= 0 && val <= 100 { Ok(val) } else { Err("Percentage out of range".to_string()) } }, Err(_) => Err("Invalid input".to_string()) }}
tamanishi
pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here match input.parse::<u8>() { Ok(p) => match p { 0..=100 => Ok(p), _ => Err("Percentage out of range".to_string()) }, Err(_) => Err("Invalid input".to_string()), }}
konishu
// pub fn parse_percentage(input: &str) -> Result<u8, String> {// // 文字列をパースして数値に変換// let parsed_percentage = match input.parse::<u8>() {// Ok(hoge) => hoge,// Err(_) => return Err("Invalid input".to_string()), // カスタムエラーメッセージ// };// // 値が範囲内にあるかチェック// if parsed_percentage > 100 {// Err("Percentage out of range".to_string())// } else {// Ok(parsed_percentage)// }// }pub fn parse_percentage(input: &str) -> Result<u8, String> { let parsed_percentage = match input.parse::<u8>() { Ok(value) => value, Err(_) => return Err("Invalid input".to_string()), }; if parsed_percentage > 100 { Err("Percentage out of range".to_string()) } else { Ok(parsed_percentage) }}
konishu
pub fn parse_percentage(input: &str) -> Result<u8, String> { // 文字列をパースして数値に変換 let parsed_percentage = match input.parse::<u8>() { Ok(hoge) => hoge, Err(_) => return Err("Invalid input".to_string()), // カスタムエラーメッセージ }; // 値が範囲内にあるかチェック if parsed_percentage > 100 { Err("Percentage out of range".to_string()) } else { Ok(parsed_percentage) }}
nichideropa
pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here match input.parse::<u8>() { Ok(percentage) if percentage <= 100 => Ok(percentage), // This handles the cases in which the before mentioned pattern // doesn't match Ok(_) => Err("Percentage out of range".to_string()), Err(_) => Err("Invalid input".to_string()), }}
jeypiti
pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here match input.parse::<u8>() { Ok(val) => { if val <= 100 { Ok(val) } else { Err("Percentage out of range".to_string()) } }, Err(_) => Err("Invalid input".to_string()) }}
0xsmarter
pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here match input.parse::<u8>() { Ok(percentage) if percentage <= 100 => Ok(percentage), Ok(_) => Err("Percentage out of range".to_string()), Err(_) => Err("Invalid input".to_string()), }}
oebelus
pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here let parsed = input.parse::<u8>(); if parsed.is_ok() { let parsed = parsed.unwrap(); if parsed > 100 { return Err("Percentage out of range".to_string()); } else { return Ok(parsed); } } else { return Err("Invalid input".to_string()); } }
tsucchinoko
pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here match input.parse::<u8>() { Ok(percentage) if percentage <= 100 => Ok(percentage), Ok(_) => Err("Percentage out of range".to_string()), Err(_) => Err("Invalid input".to_string()), }}
LauriSarap
pub fn parse_percentage(input: &str) -> Result<u8, String> { let removed_input_symbol = input.trim_end_matches('%'); match removed_input_symbol.parse::<u8>() { Ok(value) if value <= 100 => Ok(value), Ok(_) => Err(format!("Percentage out of range")), Err(_) => Err(format!("Invalid input")), }}
ankeetparikh
pub fn parse_percentage(input: &str) -> Result<u8, String> { match input.parse() { Ok(x) => if x <= 100 { Ok(x) } else { Err("Percentage out of range".to_string()) }, Err(_e) => Err("Invalid input".to_string()), }}
Nero22k
pub fn parse_percentage(input: &str) -> Result<u8, String> { let i = input.parse::<u8>(); if i.is_ok() { let i = i.unwrap(); if i > 100 { return Err("Percentage out of range".to_string()); } return Ok(i); } else { return Err("Invalid input".to_string()); }}
maxvi
pub fn parse_percentage(input: &str) -> Result<u8, String> { match input.parse::<u8>() { Ok(percentage) if percentage <= 100 => Ok(percentage), Ok(_) => Err("Percentage out of range".to_string()), Err(_) => Err("Invalid input".to_string()), }}