Created
January 29, 2021 20:57
-
-
Save justanotherdot/1b2eda4d2f32d07af8e27c766078c597 to your computer and use it in GitHub Desktop.
You may be borrowing the wrapper rather than the contents that you want.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[derive(Debug, Clone)] | |
struct Resource(usize); | |
#[derive(Debug)] | |
struct Error; | |
fn may_fail(ix: usize) -> Result<Resource, Error> { | |
if rand::random::<f64>() < 0.5 { | |
Err(Error) | |
} else { | |
Ok(Resource(ix)) | |
} | |
} | |
fn main() { | |
let mut results = Vec::with_capacity(5); | |
let mut main_resource = Resource(0); | |
for i in 0..5 { | |
let result = may_fail(i); | |
if result.is_ok() { | |
main_resource = result | |
.as_ref() | |
.unwrap() | |
.clone(); | |
} | |
// refactor. | |
// if let Ok(ref resource) = result { | |
// main_resource = resource.clone(); | |
// } | |
results.push(result); | |
} | |
dbg!(main_resource); | |
dbg!(results); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment