Skip to content

Instantly share code, notes, and snippets.

@justanotherdot
Created January 29, 2021 20:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justanotherdot/1b2eda4d2f32d07af8e27c766078c597 to your computer and use it in GitHub Desktop.
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.
#[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