fnmain() { letok_value: Result<i32, &str> = Ok(10); letvalue = ok_value.unwrap_or(0); println!("The value is: {}", value); // 输出: The value is: 10
leterr_value: Result<i32, &str> = Err("An error occurred"); letvalue = err_value.unwrap_or(0); println!("The value is: {}", value); // 输出: The value is: 0 }
1 2 3 4 5 6 7 8 9
fnmain() { letsome_value = Some(10); letvalue = some_value.unwrap_or(0); println!("The value is: {}", value); // 输出: The value is: 10
letnone_value: Option<i32> = None; letvalue = none_value.unwrap_or(0); println!("The value is: {}", value); // 输出: The value is: 0 }
fnmain() { letok_value: Result<i32, &str> = Ok(10); letvalue = ok_value.unwrap_or_else(|err| { println!("Encountered an error: {}", err); 0 }); println!("The value is: {}", value); // 输出: The value is: 10
leterr_value: Result<i32, &str> = Err("An error occurred"); letvalue = err_value.unwrap_or_else(|err| { println!("Encountered an error: {}", err); 0 }); println!("The value is: {}", value); // 输出: The value is: 0 }
1 2 3 4 5 6 7 8 9
fnmain() { letsome_value = Some(10); letvalue = some_value.unwrap_or_else(|| 0); println!("The value is: {}", value); // 输出: The value is: 10
letnone_value: Option<i32> = None; letvalue = none_value.unwrap_or_else(|| 0); println!("The value is: {}", value); // 输出: The value is: 0 }
fnmain() { letsome_value = Some(10); letvalue = some_value.unwrap_or_default(); println!("The value is: {}", value); // 输出: The value is: 10
letnone_value: Option<i32> = None; letvalue = none_value.unwrap_or_default(); println!("The value is: {}", value); // 输出: The value is: 0 }
1 2 3 4 5 6 7 8 9
fnmain() { letok_value: Result<i32, &str> = Ok(10); letvalue = ok_value.unwrap_or_default(); println!("The value is: {}", value); // 输出: The value is: 10
leterr_value: Result<i32, &str> = Err("An error occurred"); letvalue = err_value.unwrap_or_default(); println!("The value is: {}", value); // 输出: The value is: 0 }
fnmain() { letsome_value: Option<MyStruct> = Some(MyStruct { value: 10 }); letvalue = some_value.unwrap_or_default(); println!("The value is: {:?}", value); // 输出: The value is: MyStruct { value: 10 }
letnone_value: Option<MyStruct> = None; letvalue = none_value.unwrap_or_default(); println!("The value is: {:?}", value); // 输出: The value is: MyStruct { value: 42 } }
fnmain() { letok_value: Result<i32, &str> = Ok(10); letvalue = ok_value.expect("Expected Ok, but got Err"); println!("The value is: {}", value); // 输出: The value is: 10
leterr_value: Result<i32, &str> = Err("An error occurred"); letvalue = err_value.expect("Expected Ok, but got Err"); // 这里会 panic,并显示 "Expected Ok, but got Err: An error occurred" }
1 2 3 4 5 6 7 8
fnmain() { letsome_value = Some(10); letvalue = some_value.expect("Expected a value, but got None"); println!("The value is: {}", value); // 输出: The value is: 10
letnone_value: Option<i32> = None; letvalue = none_value.expect("Expected a value, but got None"); // 这里会 panic,并显示 "Expected a value, but got None" }
fnmain() { letsome_value = Some(10); letnew_value = some_value.map(|x| x * 2); println!("The new value is: {:?}", new_value); // 输出: The new value is: Some(20)
letnone_value: Option<i32> = None; letnew_value = none_value.map(|x| x * 2); println!("The new value is: {:?}", new_value); // 输出: The new value is: None }
1 2 3 4 5 6 7 8 9
fnmain() { letok_value: Result<i32, &str> = Ok(10); letnew_value = ok_value.map(|x| x * 2); println!("The new value is: {:?}", new_value); // 输出: The new value is: Ok(20)
leterr_value: Result<i32, &str> = Err("An error occurred"); letnew_value = err_value.map(|x| x * 2); println!("The new value is: {:?}", new_value); // 输出: The new value is: Err("An error occurred") }
对于Result类型,还提供了map_err()方法以适用发生错误的场景
1 2 3 4 5 6 7 8 9
fnmain() { letok_value: Result<i32, &str> = Ok(10); letnew_value = ok_value.map_err(|e| format!("Error: {}", e)); println!("The new value is: {:?}", new_value); // 输出: The new value is: Ok(10)
leterr_value: Result<i32, &str> = Err("An error occurred"); letnew_value = err_value.map_err(|e| format!("Error: {}", e)); println!("The new value is: {:?}", new_value); // 输出: The new value is: Err("Error: An error occurred") }
fnmain() { letsome_value = Some(10); letnew_value = some_value.and_then(|x| Some(x * 2)); println!("The new value is: {:?}", new_value); // 输出: The new value is: Some(20)
letnone_value: Option<i32> = None; letnew_value = none_value.and_then(|x| Some(x * 2)); println!("The new value is: {:?}", new_value); // 输出: The new value is: None }
1 2 3 4 5 6 7 8 9
fnmain() { letok_value: Result<i32, &str> = Ok(10); letnew_value = ok_value.and_then(|x| Ok(x * 2)); println!("The new value is: {:?}", new_value); // 输出: The new value is: Ok(20)
leterr_value: Result<i32, &str> = Err("An error occurred"); letnew_value = err_value.and_then(|x| Ok(x * 2)); println!("The new value is: {:?}", new_value); // 输出: The new value is: Err("An error occurred") }