rustlings-iterators3 练习题

#[derive(Debug, PartialEq, Eq)]
pub enum DivisionError {//错误类型
    NotDivisible(NotDivisibleError),
    DivideByZero,
}

#[derive(Debug, PartialEq, Eq)]
pub struct NotDivisibleError {//不能整除
    dividend: i32,
    divisor: i32,
}

// Calculate `a` divided by `b` if `a` is evenly divisible by `b`.
// Otherwise, return a suitable error.
pub fn divide(a: i32, b: i32) -> Result<i32, DivisionError> {
    return match (a,b){
        (_,0)=>Err(DivisionError::DivideByZero),
        (a,b) if a%b!=0 =>Err(DivisionError::NotDivisible(NotDivisibleError{dividend:a,divisor:b})),
        //
        _=>Ok(a/b),
    }
}

// Complete the function and return a value of the correct type so the test
// passes.
// Desired output: Ok([1, 11, 1426, 3])
fn result_with_list() ->Result<Vec<i32>,DivisionError>  {
    let numbers = vec![27, 297, 38502, 81];
    numbers.into_iter()
        .map(|n| divide(n, 27))//闭包计算结果
        .collect()//collect根据类型推断
}

// Complete the function and return a value of the correct type so the test
// passes.
// Desired output: [Ok(1), Ok(11), Ok(1426), Ok(3)]
fn list_of_results() -> Vec<Result<i32,DivisionError>> {//返回一个Vec,里面元素是Result枚举类型
    let numbers = vec![27, 297, 38502, 81];
    numbers.into_iter()
        .map(|n| divide(n, 27))
        .collect()//类型改变但是collect仍然可以自行推断
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_success() {
        assert_eq!(divide(81, 9), Ok(9));
    }

    #[test]
    fn test_not_divisible() {
        assert_eq!(
            divide(81, 6),
            Err(DivisionError::NotDivisible(NotDivisibleError {
                dividend: 81,
                divisor: 6
            }))
        );
    }

    #[test]
    fn test_divide_by_0() {
        assert_eq!(divide(81, 0), Err(DivisionError::DivideByZero));
    }

    #[test]
    fn test_divide_0_by_something() {
        assert_eq!(divide(0, 81), Ok(0));
    }

    #[test]
    fn test_result_with_list() {
        assert_eq!(format!("{:?}", result_with_list()), "Ok([1, 11, 1426, 3])");
    }

    #[test]
    fn test_list_of_results() {
        assert_eq!(
            format!("{:?}", list_of_results()),
            "[Ok(1), Ok(11), Ok(1426), Ok(3)]"
        );
    }
}

1 个赞

如果带点讲解,以及个人的收获会更好