Let's improve previous example a little bit by returning a custom error type instead of a plain string. This will allow us to define specific error types and provide more structured error handling.
The logic of the function remains the same as the previous challenge, but the returned error type is what you need to change.
Define an enum ParsePercentageError
with the following variants:
InvalidInput
: for inputs that cannot be parsed as numbers.OutOfRange
: for numbers that are not in the range 0-100.Implement the Error
trait for ParsePercentageError
. Use the std::error::Error
trait and provide human-readable descriptions for each error.
Update the parse_percentage
function to:
Ok(u8)
if the input is a valid percentage (between 0 and 100).Err(ParsePercentageError::OutOfRange)
if the number is out of range.Err(ParsePercentageError::InvalidInput)
if the input is not a valid number.std::error::Error
trait requires implementing the Display
and Debug
traits. You can derive Debug
by #[derive(Debug)]
and implement Display
manually.std::fmt
module to implement Display
for the error enum, which is required for the Error
trait.use std::error::Error;use std::fmt::{self, Display, Formatter};#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}impl Display for ParsePercentageError { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { match self { ParsePercentageError::InvalidInput => write!(f, "Input is not a valid number"), ParsePercentageError::OutOfRange => write!(f, "Percentage out of allowed range (0-100)"), } }}impl Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { match input.parse() { Ok(101..) => Err(ParsePercentageError::OutOfRange), Ok(i) => Ok(i), Err(_) => Err(ParsePercentageError::InvalidInput), }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
use std::{error::Error, fmt::{self, Display}};// 1. Finish the definition#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}impl Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{:?}", self) }}impl Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // TODO: Implement the function here input.parse::<u32>() .map_err(|_| ParsePercentageError::InvalidInput) .and_then(|num| { if num <= 100 { Ok(num as u8) } else { Err(ParsePercentageError::OutOfRange) } })}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
use std::error::Error;use std::fmt;// 1. Finish the definition#[derive(Debug, Eq, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}// 2. Implement the `Error` traitimpl Error for ParsePercentageError {}impl fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{:?}", self) }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function let number = input.trim().parse::<u8>() .map_err(|_| ParsePercentageError::InvalidInput)?; if number > 100 { Err(ParsePercentageError::OutOfRange) } else { Ok(number) }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
use std::fmt;use std::error::Error;// 1. Finish the definition#[derive(Debug, Eq, PartialEq)]pub enum ParsePercentageError{ InvalidInput, OutOfRange}impl std::fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{:?}", self) }}// 2. Implement the `Error` traitimpl Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { match input.parse().map_err(|_|ParsePercentageError::InvalidInput)? { val @ 0..=100 => Ok(val), _ => Err(ParsePercentageError::OutOfRange), }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
use std::fmt;use std::error::Error;// 1. Finish the definition#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange}// 2. Implement the `Error` traitimpl Error for ParsePercentageError {}impl fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { // Écrit la sortie formatée dans le formateur match self { ParsePercentageError::InvalidInput => write!(f, "Err(ParsePercentageError::InvalidInput)"), ParsePercentageError::OutOfRange => write!(f, "Err(ParsePercentageError::OutOfRange)"), } }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function match input.parse::<u8>() { Err(_) => Err(ParsePercentageError::InvalidInput), Ok(101..) => Err(ParsePercentageError::OutOfRange), Ok(i) => Ok(i) }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
use std::{error::Error, fmt};// 1. Finish the definition#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange}impl fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { ParsePercentageError::InvalidInput => write!(f, "Invalid input"), ParsePercentageError::OutOfRange => write!(f, "Out of range"), } }}// 2. Implement the `Error` traitimpl Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { match input.parse::<u8>() { Ok(n) => { if n <= 100 { Ok(n) } else { Err(ParsePercentageError::OutOfRange) } }, Err(_) => Err(ParsePercentageError::InvalidInput), }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
use std::{error::Error, fmt};// 1. Finish the definition#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange}impl fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { ParsePercentageError::InvalidInput => write!(f, "Invalid input"), ParsePercentageError::OutOfRange => write!(f, "Out of range"), } }}// 2. Implement the `Error` traitimpl Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { match input.parse::<u8>() { Ok(n) => { if n <= 100 { Ok(n) } else { Err(ParsePercentageError::OutOfRange) } }, Err(_) => Err(ParsePercentageError::InvalidInput), }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
use std::error;use std::fmt;// 1. Finish the definition#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}// 2. Implement the `Error` traitimpl error::Error for ParsePercentageError {}impl fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { ParsePercentageError::InvalidInput => write!(f, "Invalid input"), ParsePercentageError::OutOfRange => write!(f, "Out of range"), } }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function let result = input.parse::<u8>(); match result { Ok(n) => { if n <= 100 { Ok(n) } else { Err(ParsePercentageError::OutOfRange) } }, Err(_) => Err(ParsePercentageError::InvalidInput), }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
use std::error::Error;use std::fmt;// 1. Finish the definition#[derive(Debug,PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange}// 2. Implement the `Error` traitimpl fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "ParsePercentageError") }}impl Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function match input.parse() { Err(_) => Err(ParsePercentageError::InvalidInput), Ok(101..) => Err(ParsePercentageError::OutOfRange), Ok(u8) => Ok(u8), }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
use std::fmt;use std::error::Error;// 1. Finish the definition#[derive(Debug,PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange}// 2. Implement the `Error` traitimpl fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "ParsePercentageError") }}impl Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { match input.parse() { Err(_) => Err(ParsePercentageError::InvalidInput), Ok(101..) => Err(ParsePercentageError::OutOfRange), Ok(i) => Ok(i) }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange}impl std::fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { format!("{:?}", self).fmt(f) }}impl std::error::Error for ParsePercentageError {}// 2. Implement the `Error` traitpub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function let res: Result<u8, _> = input.parse(); match res { Ok(n) => { if n > 100 { Err(ParsePercentageError::OutOfRange) } else { Ok(n) } }, Err(_) => Err(ParsePercentageError::InvalidInput) }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
use std::error::Error;use std::fmt;// 1. Finish the definition#[derive(Debug, PartialEq, Eq)] // <== Tambahkan inipub enum ParsePercentageError { InvalidInput, OutOfRange,}// 2. Implement the `Display` traitimpl fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { ParsePercentageError::InvalidInput => write!(f, "Invalid input"), ParsePercentageError::OutOfRange => write!(f, "Percentage out of range"), } }}// 3. Implement the `Error` traitimpl Error for ParsePercentageError {}// 4. Main functionpub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { match input.parse::<u8>() { Ok(n) if n <= 100 => Ok(n), Ok(_) => Err(ParsePercentageError::OutOfRange), Err(_) => Err(ParsePercentageError::InvalidInput), }}// 5. Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Err(OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Err(InvalidInput)}
use std::error::Error;use std::fmt;// 1. Finish the definition#[derive(Debug, PartialEq, Eq)] // <== Tambahkan inipub enum ParsePercentageError { InvalidInput, OutOfRange,}// 2. Implement the `Display` traitimpl fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { ParsePercentageError::InvalidInput => write!(f, "Invalid input"), ParsePercentageError::OutOfRange => write!(f, "Percentage out of range"), } }}// 3. Implement the `Error` traitimpl Error for ParsePercentageError {}// 4. Main functionpub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { match input.parse::<u8>() { Ok(n) if n <= 100 => Ok(n), Ok(_) => Err(ParsePercentageError::OutOfRange), Err(_) => Err(ParsePercentageError::InvalidInput), }}// 5. Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Err(OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Err(InvalidInput)}
// 1. Finish the definition#[derive(Debug,PartialEq)]pub enum ParsePercentageError { OutOfRange, InvalidInput,}// 2. Implement the `Error` traitimpl std::fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { Self::InvalidInput => write!(f,"invalid input provided"), Self::OutOfRange => write!(f,"input out of range"), } }}impl std::error::Error for ParsePercentageError{}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function return match input.parse::<u8>() { Ok(t) if t <= 100 => Ok(t), Ok(_) => Err(ParsePercentageError::OutOfRange), Err(_) => Err(ParsePercentageError::InvalidInput), };}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
use std::fmt;use std::error::Error;// 1. Finish the definition#[derive(Debug)]#[derive(PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}impl fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { ParsePercentageError::InvalidInput => write!(f, "Err(ParsePercentageError::InvalidInput)"), ParsePercentageError::OutOfRange => write!(f, "Err(ParsePercentageError::OutOfRange)"), } }}impl Error for ParsePercentageError {}// 2. Implement the `Error` traitpub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function match input.parse::<i32>() { Err(_) => Err(ParsePercentageError::InvalidInput), Ok(value) if ((value < 0) | (value > 100)) => Err(ParsePercentageError::OutOfRange), Ok(value) => Ok(value as u8), }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
// 1. Finish the definition#[derive(Debug,PartialEq)]pub enum ParsePercentageError{ InvalidInput, OutOfRange,}// 2. Implement the `Error` traitimpl std::fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { Self::InvalidInput => write!(f,"invalid input provided"), Self::OutOfRange => write!(f,"input out of range"), } }}impl std::error::Error for ParsePercentageError{}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function return match input.parse::<u8>() { Ok(t) if t <= 100 => Ok(t), Ok(_) => Err(ParsePercentageError::OutOfRange), Err(_) => Err(ParsePercentageError::InvalidInput), };}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
use std::fmt;use std::error::Error;// 1. Finish the definition#[derive(Debug, Clone, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange}impl fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match *self { ParsePercentageError::InvalidInput => write!(f, "Out of range."), ParsePercentageError::OutOfRange => write!(f, "Invalid Output."), } }}// 2. Implement the `Error` traitimpl Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function let percent = input.parse::<u8>(); match percent { Ok(p) if p <= 100 => Ok(p), Ok(_) => Err(ParsePercentageError::OutOfRange), _ => Err(ParsePercentageError::InvalidInput), }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
use std::error::Error;// 1. Finish the definition#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}impl std::fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { ParsePercentageError::InvalidInput => write!(f, "Error ParsePercentageError"), ParsePercentageError::OutOfRange => write!(f, "Error out of range"), } }}// 2. Implement the `Error` traitimpl Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { match input.parse::<u8>() { Ok(n) if n <= 100 => Ok(n), Ok(_) => Err(ParsePercentageError::OutOfRange), _ => Err(ParsePercentageError::InvalidInput), }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
use std::fmt::{self, Display, Formatter};use std::error::Error;#[derive(PartialEq)]pub enum ParsePercentageError { OutOfRange, InvalidInput,}impl Display for ParsePercentageError { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { match self { ParsePercentageError::OutOfRange => write!(f, "ParsePercentageError::OutOfRange"), ParsePercentageError::InvalidInput => write!(f, "ParsePercentageError::InvalidInput"), } }}impl fmt::Debug for ParsePercentageError { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { Display::fmt(self, f) }}impl Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { match input.parse::<u8>() { Ok(p) if p <= 100 => Ok(p), Ok(_p) => Err(ParsePercentageError::OutOfRange), Err(_e) => Err(ParsePercentageError::InvalidInput) }}pub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Err(ParsePercentageError::InvalidInput)}
// 1. Finish the definition#[derive(Debug, PartialEq)]pub enum ParsePercentageError{ InvalidInput, OutOfRange}// 2. Implement the `Error` traitimpl std::fmt::Display for ParsePercentageError { fn fmt(&self, f:&mut std::fmt::Formatter) -> std::fmt::Result { match self { ParsePercentageError::InvalidInput => write!(f,"Err(ParsePercentageError::OutOfRange)"), ParsePercentageError::OutOfRange => write!(f,"Err(ParsePercentageError::InvalidInput)") } }}impl std::error::Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function let u : Result<u8, _> = input.parse(); match u { Ok(n) => if n <= 100 { Ok(n) } else { Err(ParsePercentageError::OutOfRange) } Err(_) => Err(ParsePercentageError::InvalidInput) }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
// 1. Finish the definition#[derive(Debug, PartialEq)]pub enum ParsePercentageError { OutOfRange, InvalidInput}impl std::fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { ParsePercentageError::OutOfRange => write!(f, "Out of Range"), ParsePercentageError::InvalidInput => write!(f, "Invalid Input") } }}// 2. Implement the `Error` traitimpl std::error::Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function match input.parse::<u8>() { Ok(value) => { if value > 100 { Err(ParsePercentageError::OutOfRange) } else { Ok(value) } }, Err(_) => Err(ParsePercentageError::InvalidInput), }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
use std::fmt;use std::error::Error;// 1. Finish the definition#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}// 2. Implement the `Error` traitimpl fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { ParsePercentageError::InvalidInput => write!(f, "Invalid input"), ParsePercentageError::OutOfRange => write!(f, "Percentage out of range"), } }}impl Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function match input.parse::<u8>() { Ok(value) => { if value <= 100 { Ok(value) } else { Err(ParsePercentageError::OutOfRange) } }, Err(_) => Err(ParsePercentageError::InvalidInput), }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
// 1. Finish the definitionuse std::error::Error;use std::fmt::Display;#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange}// 2. Implement the `Error` traitimpl Error for ParsePercentageError {}impl Display for ParsePercentageError { fn fmt(&self, f : &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{}", match self { ParsePercentageError::OutOfRange => "Out of range.", ParsePercentageError::InvalidInput => "Invalid Input" }) }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { match input.parse::<u8>() { Ok(n) if n <= 100 => Ok(n), Ok(_) => Err(ParsePercentageError::OutOfRange), Err(_) => Err(ParsePercentageError::InvalidInput) }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
// 1. Finish the definitionuse std::{error::Error, fmt::Display};#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}// 2. Implement the `Error` traitimpl Error for ParsePercentageError {}impl Display for ParsePercentageError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!( f, "{}", match self { ParsePercentageError::InvalidInput => "Invalid input", ParsePercentageError::OutOfRange => "Value above 100", } ) }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { match input.parse::<u8>() { Ok(n) if n <= 100 => Ok(n), Ok(_) => Err(ParsePercentageError::OutOfRange), Err(_) => Err(ParsePercentageError::InvalidInput), }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
use std::error::Error;use std::fmt::Display;#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}// 2. Implement the `Error` traitimpl Display for ParsePercentageError { fn fmt(&self, _f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { todo!() }}impl Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { let number = input .parse::<u8>() .map_err(|_| ParsePercentageError::InvalidInput)?; match number { 0..=100 => Ok(number), _ => Err(ParsePercentageError::OutOfRange), }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
// 1. Finish the definition#[derive(Debug, PartialEq)]pub enum ParsePercentageError { OutOfRange, InvalidInput}// 2. Implement the `Error` traitimpl std::error::Error for ParsePercentageError {}impl std::fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { match self { ParsePercentageError::OutOfRange => write!(f, "Value out of range"), ParsePercentageError::InvalidInput => write!(f, "Invalid input"), } }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function match input.parse::<u8>() { Ok(value) => { if value <= 100 { Ok(value) } else { Err(ParsePercentageError::OutOfRange) } }, Err(_) => Err(ParsePercentageError::InvalidInput) }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
// 1. Finish the definition#[derive(Debug, PartialEq)]pub enum ParsePercentageError{ InvalidInput, OutOfRange,}// 2. Implement the `Error` traitimpl std::error::Error for ParsePercentageError{}impl std::fmt::Display for ParsePercentageError{ fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { match self{ ParsePercentageError::InvalidInput => write!(f, "Invalid Input"), ParsePercentageError::OutOfRange => write!(f, "Out of"), } }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function match input.parse::<u8>(){ //Don't have to compare if it is less than 0 because it is u8 Ok(num) if num <= 100 => Ok(num), Ok(_) => Err(ParsePercentageError::OutOfRange), Err(_) => Err(ParsePercentageError::InvalidInput), }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
use std::error::Error;use std::fmt;use std::str::FromStr;// 1. Finish the definition#[derive(Debug, PartialEq, Eq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}impl fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { ParsePercentageError::InvalidInput => write!(f, "Invalid input"), ParsePercentageError::OutOfRange => write!(f, "Percentage value is out of the range"), } }}// 2. Implement the `Error` traitimpl Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { match i32::from_str(input) { Ok(n) if n >= 0 && n <= 100 => Ok(n as u8), Ok(_) => Err(ParsePercentageError::OutOfRange), Err(_) => Err(ParsePercentageError::InvalidInput), }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
use std::error::Error;use std::fmt;use std::str::FromStr;// 1. Finish the definition#[derive(Debug, PartialEq, Eq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}impl fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { ParsePercentageError::InvalidInput => write!(f, "Invalid input"), ParsePercentageError::OutOfRange => write!(f, "Percentage value is out of the range"), } }}// 2. Implement the `Error` traitimpl Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { match i32::from_str(input) { Ok(n) if n >= 0 && n <= 100 => Ok(n as u8), Ok(_) => Err(ParsePercentageError::OutOfRange), Err(_) => Err(ParsePercentageError::InvalidInput), }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
use std::{error::Error, fmt::Display, };use std::fmt;// 1. Finish the definition#[derive(Debug, PartialEq)]pub enum ParsePercentageError{ InvalidInput, OutOfRange,}// 2. Implement the `Error` traitimpl Error for ParsePercentageError{}impl Display for ParsePercentageError{ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self{ ParsePercentageError::InvalidInput => write!(f, "Invalid Input"), ParsePercentageError::OutOfRange => write!(f, "Out of"), } }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function input .parse::<u8>() .map_err(|_| ParsePercentageError::InvalidInput) .and_then(|num| { if num > 100 || num < 0{ Err(ParsePercentageError::OutOfRange) }else{ Ok(num) } })}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
use std::{error::Error, fmt::Display};#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}impl Error for ParsePercentageError {}impl Display for ParsePercentageError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { ParsePercentageError::InvalidInput => write!(f, "Invalid input"), ParsePercentageError::OutOfRange => write!(f, "Percentage out of range"), } }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { input .parse::<u8>() .map_err(|_| ParsePercentageError::InvalidInput) .and_then(|num| { if num > 100 { Err(ParsePercentageError::OutOfRange) } else { Ok(num) } })}// Example usagepub fn main() { let result = parse_percentage("50"); assert_eq!(result, Ok(50)); let result = parse_percentage("101"); assert_eq!(result, Err(ParsePercentageError::OutOfRange)); let result = parse_percentage("abc"); assert_eq!(result, Err(ParsePercentageError::InvalidInput));}
use std::error::Error;use std::fmt::{Display};// 1. Finish the definition#[derive(Debug, PartialEq)]pub enum ParsePercentageError { OutOfRange, InvalidInput,}// 2. Implement the `Error` traitimpl Error for ParsePercentageError {}impl Display for ParsePercentageError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{}", dbg!(self)) }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { match input.parse::<u8>() { Ok(num) if num <= 100 => Ok(num), Ok(_) => Err(ParsePercentageError::OutOfRange), Err(_) => Err(ParsePercentageError::InvalidInput), }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
use std::error::Error;use std::fmt::{self, Debug};#[derive(PartialEq)]pub enum ParsePercentageError{ InvalidInput, OutOfRange,}impl fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self{ ParsePercentageError::OutOfRange => {write!(f, "ParsePercentageError::OutOfRange")}, ParsePercentageError::InvalidInput => {write!(f, "ParsePercentageError::InvalidInput")}, } }}impl Debug for ParsePercentageError{ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{self}") }}impl Error for ParsePercentageError{}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { match input.parse::<u8>() { Ok(n @ 0..=100) => Ok(n), Ok(_) => Err(ParsePercentageError::OutOfRange), Err(_) => Err(ParsePercentageError::InvalidInput), } }// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
use std::error::Error;use std::fmt::Display;// 1. Finish the definition#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}// 2. Implement the `Error` traitimpl Error for ParsePercentageError {}impl Display for ParsePercentageError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{}", dbg!(self)) }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { match input.parse::<u8>() { Ok(n) if n <= 100 => Ok(n), Ok(_) => Err(ParsePercentageError::OutOfRange), Err(_) => Err(ParsePercentageError::InvalidInput), }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
use std::error::Error;use std::fmt::Display;// 1. Finish the definition#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}// 2. Implement the `Error` traitimpl Error for ParsePercentageError {}impl Display for ParsePercentageError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{}", dbg!(self)) }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { let number: u8 = match input.parse::<u8>() { Ok(n) => n, Err(_) => { return Err(ParsePercentageError::InvalidInput); } }; match number <= 100 { true => Ok(number), false => Err(ParsePercentageError::OutOfRange), }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
// 1. Finish the definitionuse std::error::Error;use std::fmt;#[derive(Debug, PartialEq)]pub enum ParsePercentageError{ InvalidInput, OutOfRange,}// 2. Implement the `Error` traitimpl fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { ParsePercentageError::InvalidInput => write!(f, "Invalid input: not a valid number"), ParsePercentageError::OutOfRange => write!(f, "Value out of range: must be between 0 and 100"), } }}impl Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function match input.trim().parse::<u8>(){ Ok(value) if value <= 100=> Ok(value), Ok(_)=> Err(ParsePercentageError::OutOfRange), Err(_)=>Err(ParsePercentageError::InvalidInput), }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
// 1. Finish the definitionuse std::error::Error;use std::fmt;#[derive(Debug, PartialEq)]pub enum ParsePercentageError{ InvalidInput, OutOfRange,}// 2. Implement the `Error` traitimpl fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { ParsePercentageError::InvalidInput => write!(f, "Invalid input: not a valid number"), ParsePercentageError::OutOfRange => write!(f, "Value out of range: must be between 0 and 100"), } }}impl Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function match input.trim().parse::<u8>(){ Ok(value) if value <= 100=> Ok(value), Ok(_)=> Err(ParsePercentageError::OutOfRange), Err(_)=>Err(ParsePercentageError::InvalidInput), }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
use std::{error::Error, fmt::{self, Display}};// 1. Finish the definition#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}impl Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { ParsePercentageError::InvalidInput => write!(f, "Invalid input"), ParsePercentageError::OutOfRange => write!(f, "Percentage out of range"), } }}// 2. Implement the `Error` traitimpl Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { match input.parse() { Err(_) => Err(ParsePercentageError::InvalidInput), Ok(number) if number <= 100 => Ok(number), Ok(_) => Err(ParsePercentageError::OutOfRange), }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
use std::{error::Error, fmt::Display};// 1. Finish the definition#[derive(Debug, PartialEq)]pub enum ParsePercentageError { OutOfRange, InvalidInput,}// 2. Implement the `Error` traitimpl Display for ParsePercentageError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { Self::InvalidInput => write!(f, "InvalidInput"), Self::OutOfRange => write!(f, "OutOfRange"), } }}impl Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function match input.parse::<u8>() { Ok(n @ 0..=100) => Ok(n), Ok(_) => Err(ParsePercentageError::OutOfRange), Err(_) => Err(ParsePercentageError::InvalidInput), }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
use std::fmt;use std::error::Error;// 1. Finish the definition#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}// 2. Implement the `Error` traitimpl fmt::Display for ParsePercentageError { fn fmt (&self, f: &mut fmt::Formatter) -> fmt::Result { match self { Self::InvalidInput => write!(f, "Invalid input"), Self::OutOfRange => write!(f, "Out of range"), } }}impl Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function let number = match input.parse::<u8>() { Ok(number) => number, Err(_e) => return Err(ParsePercentageError::InvalidInput) }; if number > 100 { return Err(ParsePercentageError::OutOfRange); } Ok(number)}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
use std::fmt;// 1. Finish the definition#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}impl fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { Self::InvalidInput => write!(f, "Invalid input"), Self::OutOfRange => write!(f, "Out of range") } }}// 2. Implement the `Error` traitimpl std::error::Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function let res: u8 = input.parse().map_err(|_| ParsePercentageError::InvalidInput)?; if res > 100 { return Err(ParsePercentageError::OutOfRange); } Ok(res)}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
use std::fmt;// 1. Finish the definition#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}impl fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { Self::InvalidInput => write!(f, "Invalid input"), Self::OutOfRange => write!(f, "Out of range") } }}// 2. Implement the `Error` traitimpl std::error::Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function input.parse::<u8>() .map_err(|_| ParsePercentageError::InvalidInput) .and_then(|n| { (n <= 100) .then_some(n) .ok_or(ParsePercentageError::OutOfRange) })}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
use std::fmt;use std;// 1. Finish the definition#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange}// 2. Implement the `Error` traitimpl fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { ParsePercentageError::InvalidInput => write!(f, "Invalid input"), ParsePercentageError::OutOfRange => write!(f, "Percentage out of range"), } }}impl std::error::Error for ParsePercentageError { }pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function let input: u8 = input.parse().map_err(|_| ParsePercentageError::InvalidInput)?; if input > 100 { return Err(ParsePercentageError::OutOfRange) } return Ok(input) }// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
use std::error::Error;use std::fmt;// 1. Finish the definition#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange}// 2. Implement the `Error` traitimpl fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { ParsePercentageError::InvalidInput => write!(f, "The input is not a valid number"), ParsePercentageError::OutOfRange => write!(f, "The value is outside 0 - 100") } }}impl Error for ParsePercentageError { }pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function match input.parse::<u8>() { Ok(n) => { if n > 100 { Err(ParsePercentageError::OutOfRange) } else { Ok(n) } }, Err(_) => Err(ParsePercentageError::InvalidInput) }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
use std::{error::Error, fmt};// 1. Finish the definition#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}// 2. Implement the `Error` traitimpl fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let err_msg = match self { ParsePercentageError::InvalidInput => ParsePercentageError::InvalidInput, ParsePercentageError::OutOfRange => ParsePercentageError::OutOfRange, }; write!(f, "Err({err_msg})") }}impl Error for ParsePercentageError {}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { match input.parse() { Ok(num) => { if num <= 100 { Ok(num) } else { Err(ParsePercentageError::OutOfRange) } }, Err(_) => Err(ParsePercentageError::InvalidInput), }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
use std::error::Error;use std::fmt;// 1. Finish the definition#[derive(Debug, PartialEq)] pub enum ParsePercentageError { InvalidInput, OutOfRange}// 2. Implement the `Error` traitimpl Error for ParsePercentageError {}impl fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { let err_msg = match self { ParsePercentageError::InvalidInput => "ParsePercentageError::InvalidInput", ParsePercentageError::OutOfRange => "ParsePercentageError::OutOfRange", }; write!(f, "{}", err_msg) }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function match input.parse::<u8>() { Ok(percentage) => { if percentage > 100 { Err(ParsePercentageError::OutOfRange) } else { Ok(percentage) } } _ => Err(ParsePercentageError::InvalidInput) }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
use std::error::Error;use std::fmt;// 1. Finish the definition#[derive(Debug, PartialEq)] pub enum ParsePercentageError { InvalidInput, OutOfRange}// 2. Implement the `Error` traitimpl Error for ParsePercentageError {}impl fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { let err_msg = match self { ParsePercentageError::InvalidInput => "ParsePercentageError::InvalidInput", ParsePercentageError::OutOfRange => "ParsePercentageError::OutOfRange", }; write!(f, "{}", err_msg) }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function match input.parse::<u8>() { Ok(percentage) => { if percentage > 100 { Err(ParsePercentageError::OutOfRange) } else { Ok(percentage) } } _ => Err(ParsePercentageError::InvalidInput) }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
use std::error::Error;use std::fmt;// 1. Finish the definition#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}// 2. Implement the `Error` traitimpl Error for ParsePercentageError {}impl fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { let err_msg = match self { ParsePercentageError::InvalidInput => "ParsePercentageError::InvalidInput", ParsePercentageError::OutOfRange => "ParsePercentageError::OutOfRange", }; write!(f, "{}", err_msg) }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function match input.parse::<u8>() { Ok(percentage) => { if percentage <= 100 { Ok(percentage) } else { Err(ParsePercentageError::OutOfRange) } }, _ => Err(ParsePercentageError::InvalidInput) }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
use std::error::Error;use std::fmt;// 1. Finish the definition#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange}// 2. Implement the `Error` traitimpl Error for ParsePercentageError {}impl fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { ParsePercentageError::InvalidInput => write!(f, "Invalid input"), ParsePercentageError::OutOfRange => write!(f, "Percentage out of range"), } }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { // 3. Implement this function match input.parse::<u8>() { Ok(n) => if n<= 100 { Ok(n) } else { Err(ParsePercentageError::OutOfRange)}, Err(_) => Err(ParsePercentageError::InvalidInput) }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}
use std::error::Error;use std::fmt;// 1. Finish the definition#[derive(Debug, PartialEq)]pub enum ParsePercentageError { InvalidInput, OutOfRange,}// 2. Implement the `Error` traitimpl Error for ParsePercentageError {}impl fmt::Display for ParsePercentageError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { ParsePercentageError::InvalidInput => write!(f, "Invalid input"), ParsePercentageError::OutOfRange => write!(f, "Percentage out of range"), } }}pub fn parse_percentage(input: &str) -> Result<u8, ParsePercentageError> { let result = input.parse::<u8>(); match result { Ok(num) => if num <= 100 { Ok(num) } else {Err(ParsePercentageError::OutOfRange)}, Err(_) => Err(ParsePercentageError::InvalidInput), }}// Example usagepub fn main() { let result = parse_percentage("50"); println!("{:?}", result); // Should print: Ok(50) let result = parse_percentage("101"); println!("{:?}", result); // Should print: Err(ParsePercentageError::OutOfRange) let result = parse_percentage("abc"); println!("{:?}", result); // Should print: Err(ParsePercentageError::InvalidInput)}