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 challenges, 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".carlos-quantexai
pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here let parsed_value: Result<u8, _> = input.parse(); // Store the parsed value if let Ok(value) = parsed_value { if value >= 0 && value <= 100 { // Use logical AND return Ok(value); } else { return Err("Percentage out of range".to_string()); } } else { return Err("Invalid input".to_string()); }}
Maki-SIO
pub fn parse_percentage(input: &str) -> Result<u8, String> { let mut num: u8 = 0; match input.parse::<u8>() { Ok(v) => num = v, Err(e) => return Err(String::from("Invalid input")), }; if num > 100 || num < 0 { return Err(String::from("Percentage out of range")); } else { return Ok(num); }}
Mxn-ptr
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()) }}
yansq
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) if n > 100 => Err("Percentage out of range".to_string()), Ok(n) => Ok(n) }}
zelsazgh
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(format!("Percentage out of range")), Err(_) => Err(format!("Invalid input")), }}
funny233-github
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(format!("Percentage out of range")), Err(_) => Err(format!("Invalid input")), }}
jose-bernardo
pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here match input.parse::<u8>() { Ok(number) if number > 100 => Err("Percentage out of range".to_string()), Ok(number) => Ok(number), Err(_e) => Err("Invalid input".to_string()), }}
digitalresistor
pub fn parse_percentage(input: &str) -> Result<u8, String> { match input.trim().parse::<u8>() { Ok(n) if n > 100 => Err("Percentage out of range".to_string()), Ok(n) => Ok(n), Err(_) => Err("Invalid input".to_string()), }}
Karivelia
pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here if let Ok(x) = input.parse::<u8>(){ if x > 100 { return Err("Percentage out of range".to_string()); } return Ok(x); } Err("Invalid input".to_string())}
radloffl
pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here match input.parse::<u8>() { Ok(i) if i < 101 => Ok(i), Err(_) => Err("Invalid input".to_string()), _ => Err("Percentage out of range".to_string()) }}
majesticalcreature
pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here let parsed = input.parse(); let n: u8; match parsed { Ok(num) => n = num, Err(_) => return Err(String::from("Invalid input")) } if n > 100 { return Err(String::from("Percentage out of range")); } Ok(n)}
jhq223
pub fn parse_percentage(input: &str) -> Result<u8, String> { match input.parse::<u8>() { Ok(n) => { if n <= 100 { Ok(n) } else { Err("Percentage out of range".to_string()) } } Err(_) => { Err("Invalid input".to_string()) } }}
xbarnett
pub fn parse_percentage(input: &str) -> Result<u8, String> { let Ok(n) = input.parse::<u8>() else { return Err(String::from("Invalid input")) }; if 0 <= n && n <= 100 { Ok(n) } else { Err(String::from("Percentage out of range")) }}
mk-comm
pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here let parsed= input.parse(); let parsed = match parsed { Ok(value) => value, Err(_) => return Err("Invalid input".to_string()) } ; if parsed > 100 { return Err("Percentage out of range".to_string()) } Ok(parsed)}
Johnchoi913
pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here let input_result = input.parse::<u8>(); match input_result { Err(_) => return Err("Invalid input".to_string()), Ok(T) => { if T <= 100 { return Ok(T); } else { return Err("Percentage out of range".to_string()); } }, }}
LaurentiuStoleriu
pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here let percent = input.parse::<u8>(); match percent { Ok(value) => { if value > 100u8 { Err("Percentage out of range".to_string()) } else { Ok(value) } }, Err(_) => Err("Invalid input".to_string()) } }
matei
pub fn parse_percentage(input: &str) -> Result<u8, String> { match input.trim().parse::<u8>() { Ok(num) if num <= 100 => Ok(num), Ok(_) => Err(String::from("Percentage out of range")), Err(_) => Err(String::from("Invalid input")) }}
sander-b-postnl
pub fn parse_percentage(input: &str) -> Result<u8, String> { match input.parse::<u8>() { Ok(n) if n <= 100 => Ok(n), Ok(_) => Err("Percentage out of range".to_string()), Err(_) => Err("Invalid input".to_string()), }}
amassare
pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here match input.parse(){ Err(_)=>Err(String::from("Invalid input")), Ok(num@0..=100)=>Ok(num), Ok(_)=>Err(String::from("Percentage out of range")) }}
alexromanov
pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here match input.parse::<u8>() { Ok(n) if (0..=100).contains(&n) => Ok(n), Ok(_) => Err("Percentage out of range".to_string()), Err(_) => Err("Invalid input".to_string()), }}
IdoPort
pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here match input.parse() { Ok(num) => if num <= 100 {Ok(num)} else {Err("Percentage out of range".to_string())} _ => Err("Invalid input".to_string()) }}
Ustin
pub fn parse_percentage(input: &str) -> Result<u8, String> { match input.parse() { Ok(num) => if num <= 100 {Ok(num)} else {Err("Percentage out of range".to_string())}, _ => Err("Invalid input".to_string()) }}
kyhou
pub fn parse_percentage(input: &str) -> Result<u8, String> { if let Ok(n) = input.parse() { match n { n if n >= 0 && n <= 100 => Ok(n), _ => Err("Percentage out of range".to_string()), } } else { Err("Invalid input".to_string()) }}
Aditeya
pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here let Ok(num) = u8::from_str_radix(input, 10) else { return Err("Invalid input".to_string()); }; if num > 100 { return Err("Percentage out of range".to_string()); } Ok(num)}
CianciuStyles
pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here match input.parse::<u8>() { Ok(valid) if (0..=100).contains(&valid) => Ok(valid), Ok(_) => Err(String::from("Percentage out of range")), Err(_) => Err(String::from("Invalid input")) }}
ayushrawat10
pub fn parse_percentage(input: &str) -> Result<u8, String> { match input.parse::<u8>() { Ok(n) if (0..=100).contains(&n) => Ok(n), Ok(_) => Err("Percentage out of range".to_string()), Err(_) => Err("Invalid input".to_string()), }}
pbjarterot
pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here match input.parse::<u8>() { Ok(num) => { if num >= 0 && num <= 100 { return Ok(num) } else { return Err("Percentage out of range".to_string()) } } Err(_) => return Err("Invalid input".to_string()) }}
masteryachty
pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here let percent = input.parse::<u8>(); match percent { Ok(value) => { if value > 100u8 { Err("Percentage out of range".to_string()) } else { Ok(value) } }, Err(_) => Err("Invalid input".to_string()) } }
damascussteel21
pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here if let Ok(perc) = input.parse::<u8>() { if perc <= 100 { Ok(perc) } else { Err(String::from("Percentage out of range")) } } else { Err(String::from("Invalid input")) }}
jtruong04
pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here let percent = input.parse::<u8>(); match percent { Ok(value) => { if value > 100u8 { Err("Percentage out of range".to_string()) } else { Ok(value) } }, Err(_) => Err("Invalid input".to_string()) } }
abhiyan-chhetri
pub fn parse_percentage(input: &str) -> Result<u8, String> { match input.trim().parse::<u8>() { Ok(value) if value <= 100 => Ok(value), Ok(_) => Err("Percentage out of range".to_string()), _ => Err("Invalid input".to_string()), }}
oneopane
pub fn parse_percentage(input: &str) -> Result<u8, String> { match input.trim().parse::<u8>() { Ok(value) if value <= 100 => Ok(value), Ok(_) => Err("Percentage out of range".to_string()), _ => Err("Invalid input".to_string()), }}
edgarcnp
pub fn parse_percentage(input: &str) -> Result<u8, String> { match input.trim().parse::<u8>() { Ok(value) if value <= 100 => Ok(value), Ok(_) => Err("Percentage out of range".to_string()), _ => Err("Invalid input".to_string()) }}
Sommos
pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here let percent = input.parse::<u8>().map_err(|_| "Invalid input".to_string())?; if percent > 100 { Err("Percentage out of range".to_string()) } else { Ok(percent) }}
wischi-chr
pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here let percent = input.parse::<u8>().map_err(|_| "Invalid input".to_string())?; if percent > 100 { Err("Percentage out of range".to_string()) } else { Ok(percent) }}
tukantje
pub fn parse_percentage(input: &str) -> Result<u8, String> { input .parse::<u32>() .map_err(|_| "Invalid input".to_string()) .and_then(|num| match num { 0..=100 => Ok(num as u8), _ => Err("Percentage out of range".to_string()) })}
Ignition
pub fn parse_percentage(input: &str) -> Result<u8, String> { input .parse::<u32>() .map_err(|_| "Invalid input".to_string()) .and_then(|num| { match num { 0..=100 => Ok(num as u8), _ => Err("Percentage out of range".to_string()) } })}
dyoo
pub fn parse_percentage(input: &str) -> Result<u8, String> { let p: u8 = input.parse().map_err(|_| "Invalid input".to_string())?; if p <= 100 { Ok(p) } else { Err("Percentage out of range".to_string()) } // TODO: Implement the function here}
jaswgreen
pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here match input.parse::<u8>() { Ok(percentage) => if percentage > 100 { Err("Percentage out of range".to_string()) } else { Ok(percentage) }, _ => Err("Invalid input".to_string()) }}
hilias
pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here match input.parse() { Ok(percentage) => if percentage > 100 { Err("Percentage out of range".to_string()) } else { Ok(percentage) }, Err(_) => Err("Invalid input".to_string()) }}
CarrotTarrot
pub fn parse_percentage(input: &str) -> Result<u8, String> { let number = match input.parse::<u8>() { Ok(number) => number, Err(_) => return Err("Invalid input".to_string()) }; if number > 100 { return Err("Percentage out of range".to_string()); } Ok(number)}
qcabanes-hobby
pub fn parse_percentage(input: &str) -> Result<u8, String> { if let Ok(n) = input.parse() { match n { 0..=100 => Ok(n), _ => Err("Percentage out of range".to_string()), } } else { Err("Invalid input".to_string()) }}
TaiPoole
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()) }}
hinphansa
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 as u8), Ok(_) => Err("Percentage out of range".to_string()), _ => Err("Invalid input".to_string()), }}
hinphansa
pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here match input.parse::<u8>() { Ok(n) => { if n > 100 { Err("Percentage out of range".to_string()) } else { Ok(n) } } Err(_) => Err("Invalid input".to_string()), }}
hinphansa
pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here match input.parse() as Result<u8, _> { Ok(x) => { if x > 100 { return Err("Percentage out of range".to_string()); } return Ok(x); } Err(_) => { return Err("Invalid input".to_string()); } }}
TomBaston
pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here match input.parse::<u8>() { Ok(n) => match n { 0..=100 => Ok(n), _ => Err("Percentage out of range".into()) }, _ => Err("Invalid input".into()) }}
Shorakie
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(String::from("Percentage out of range")), Err(_) => Err(String::from("Invalid input")), }}
Shorakie
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(number) => Err(String::from("Percentage out of range")), Err(_) => Err(String::from("Invalid input")), }}
swandyr
pub fn parse_percentage(input: &str) -> Result<u8, String> { // TODO: Implement the function here match input.parse() { Err(_) => Err("Invalid input".into()), Ok(n) if n > 100 => Err("Percentage out of range".into()), Ok(n) => Ok(n), }}