I have a struct that has a new() method with a signature like this:
pub fn new(a: i32) -> Result<Rocket, RocketErr>
When I call the new() method I want to use the actual object if it's good, or just ignore it if it throws an error.
I came up with some sample code that should help explain what I mean. I thought I might get away by matching the result as many time as I need it, but the variable gets moved so I can't use it that way. I can't figure out how to do it.
In C it would be something like
Rocket *rocket = NewRocket();
if (rocket != NULL) {
// do something with it
}
// do some more stuff
// use it again if available
if (rocket != NULL) {
// do something with it
}
let mut rocket = Rocket::new();
if let Ok(r) = &mut rocket {
// `r` is a `&mut Rocket` borrow here
}
/// ...
if let Ok(r) = &mut rocket {
// `r` is a `&mut Rocket` borrow again
}
/// ...
// may want to clean up by value now?
match rocket {
Ok(mut r) => // owned rocket ...
Err(e) => // owned error ...
}
Thank you all for the suggestions guys. I don't know which one is more idiomatic in Rust, but for the time being I went with @vitalyd solution. Cheers!