1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| fn match_result_1(result: Result<i32, bool>) -> bool { match result { Ok(val) => { if val == 1 { return true; } else { return false; } } Err(b) => return false, }; }
fn unwrap_result_1(result: Result<i32, bool>) -> bool { let val = result.unwrap(); if val == 1 { return true; } else { return false; } }
fn if_let_result_1(result: Result<i32, bool>) -> bool { if let Ok(1) = result { return true; } else { return false; } }
#[cfg(test)] mod test { use super::*;
#[test] fn test_match_result_1() { assert!(match_result_1(Ok(1))); assert!(!match_result_1(Ok(2))); assert!(!match_result_1(Err(true))); assert!(!match_result_1(Err(false))); } #[test] fn test_unwrap_result_1() { assert!(unwrap_result_1(Ok(1))); assert!(!unwrap_result_1(Ok(2))); assert!(!unwrap_result_1(Err(true))); assert!(!unwrap_result_1(Err(false))); } #[test] fn test_if_let_result_1() { assert!(if_let_result_1(Ok(1))); assert!(!if_let_result_1(Ok(2))); assert!(!if_let_result_1(Err(true))); assert!(!if_let_result_1(Err(false))); } }
|