Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

?

match를 사용하여 결과를 체인으로 연결하는 것은 꽤 지저분해질 수 있습니다. 다행히 ? 연산자를 사용하여 다시 깔끔하게 만들 수 있습니다. ?Result를 반환하는 표현식의 끝에 사용되며, match 표현식과 동일하게 작동합니다. 여기서 Err(err) 가지는 조기 리턴인 return Err(From::from(err))로 확장되고, Ok(ok) 가지는 ok 표현식으로 확장됩니다.

mod checked {
    #[derive(Debug)]
    enum MathError {
        DivisionByZero,
        NonPositiveLogarithm,
        NegativeSquareRoot,
    }

    type MathResult = Result<f64, MathError>;

    fn div(x: f64, y: f64) -> MathResult {
        if y == 0.0 {
            Err(MathError::DivisionByZero)
        } else {
            Ok(x / y)
        }
    }

    fn sqrt(x: f64) -> MathResult {
        if x < 0.0 {
            Err(MathError::NegativeSquareRoot)
        } else {
            Ok(x.sqrt())
        }
    }

    fn ln(x: f64) -> MathResult {
        if x <= 0.0 {
            Err(MathError::NonPositiveLogarithm)
        } else {
            Ok(x.ln())
        }
    }

    // 중간 함수
    fn op_(x: f64, y: f64) -> MathResult {
        // 만약 `div`가 "실패"하면, `DivisionByZero`가 반환됩니다
        let ratio = div(x, y)?;

        // 만약 `ln`가 "실패"하면, `NonPositiveLogarithm`이 반환됩니다
        let ln = ln(ratio)?;

        sqrt(ln)
    }

    pub fn op(x: f64, y: f64) {
        match op_(x, y) {
            Err(why) => panic!("{}", match why {
                MathError::NonPositiveLogarithm
                    => "0 이하의 수에 대한 로그",
                MathError::DivisionByZero
                    => "0으로 나누기",
                MathError::NegativeSquareRoot
                    => "음수의 제곱근",
            }),
            Ok(value) => println!("{}", value),
        }
    }
}

fn main() {
    checked::op(1.0, 10.0);
}

Result를 매핑하거나 구성(compose)하는 많은 메서드들이 있으니, 반드시 문서를 확인해 보세요.