//! Learning how to document and publish my own crate. //! //! This is just a library with the basic math operations. /// Defines math errors you can get when dividing #[derive(Debug)] pub enum MathError { DivisionByZero, ZeroDividedByZero, } /// Adds two numbers /// /// # Examples /// /// ``` /// let result = daniels_basic_math::add(2, 2); /// assert_eq!(result, 4); /// ``` pub fn add(left: usize, right: usize) -> usize { left + right } /// Multiplies two numbers /// /// # Examples /// /// ``` /// let result = daniels_basic_math::multiply(2, 3); /// assert_eq!(result, 6); /// ``` pub fn multiply(left: usize, right: usize) -> usize { left * right } /// Subtracts two numbers /// /// # Examples /// /// ``` /// let result = daniels_basic_math::subtract(2, 2); /// assert_eq!(result, 0); /// ``` pub fn subtract(left: usize, right: usize) -> usize { left - right } /// Divides the first argument over the second /// /// # Errors /// /// - Passing a zero as the right argument leds to a MathError::DivisionByZero error. /// - Passing zero as both arguments results in a MathError::ZeroDividedByZero error. /// /// # Examples /// /// ``` /// let result = daniels_basic_math::divide(2, 2); /// assert!(matches!(result, Ok(1))); /// /// let result = daniels_basic_math::divide(2, 0); /// assert!(matches!(result, Err(daniels_basic_math::MathError::DivisionByZero))); /// /// let result = daniels_basic_math::divide(0, 0); /// assert!(matches!(result, Err(daniels_basic_math::MathError::ZeroDividedByZero))); /// ``` pub fn divide(left: usize, right: usize) -> Result { if left!=0 && right==0 { return Err(MathError::DivisionByZero); } if left==0 && right==0 { return Err(MathError::ZeroDividedByZero); } Ok(left/right) }