Hello.
I am looking for a function that will be the opposite of the unwrap ()
function.
I would like to conveniently write unit tests.
-
Is such a feature in the standard library?
-
I made a function like this:
pub fn unwrap_err<T, E>(result: std::result::Result<T, E>) -> E {
match result
{
Ok(_) => { panic!("should be error!"); },
Err(e) => { return e; }
};
}
- How do you convert it (
unwrap_err
) into a feature and add it to the structure?
// ???
pub trait UnwrapError<T, E>
{
fn unwrap_error(self, _: T, e: E) -> E
where Self: Sized
{
let x = unwrap_err(self);
return x;
}
}
fn main()
{
let x = ... ; // my struct
let err = x.unwrap_error(); // panic if OK
}