use std::fmt; // Import the `fmt` module. #[derive(Debug)] struct DivideError { details: String } impl DivideError { fn new(msg: &str) -> DivideError { DivideError{details: msg.to_string()} } } impl fmt::Display for DivideError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f,"{}",self.details) } } impl std::error::Error for DivideError { fn description(&self) -> &str { &self.details } } fn divide(a: i32, b: i32) -> Result { if b != 0 { Ok(a / b) } else { Err(DivideError::new("can not devide 0")) } } fn divideWrap() -> Result { let result = divide(10, 5)?; println!("{}", result); Ok(()) } fn main() { divideWrap(); }
Tham khảo: https://stackoverflow.com/questions/42917566/what-is-this-question-mark-operator-about