To “unwrap” something in Rust is to say, “Give me the result of the computation, and if there was an error, panic and stop the program.” It would be better if we showed the code for unwrapping because it is so simple, but to do that, we will first need to explore the Option and Result types.
It is an enum with the variants, Ok(T) , representing success and containing a value, and Err(E) , representing error and containing an error value. Functions return Result whenever errors are expected and recoverable.
If you read the documentation for Result::unwrap
, you'll note that it's under a little section called:
impl<T, E> Result<T, E>
where E: Debug
This means the methods in that section only exist so long as the given constraints are satisfied.
The only reason unwrap
wouldn't exist is that Error
doesn't implement Debug
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With