Alexander Fadeev's answer is correct, but there's a bit more information from Rust Book (Ch 9):
It would also be appropriate to call unwrap
when you have some other logic that ensures the Result
will have an Ok
value, but the logic isn’t something the compiler understands. You’ll still have a Result
value that you need to handle: whatever operation you’re calling still has the possibility of failing in general, even though it’s logically impossible in your particular situation. If you can ensure by manually inspecting the code that you’ll never have an Err
variant, it’s perfectly acceptable to call unwrap
. Here’s an example:
use std::net::IpAddr;
let home: IpAddr = "127.0.0.1".parse().unwrap();
We’re creating an IpAddr
instance by parsing a hardcoded string. We can see that 127.0.0.1
is a valid IP address, so it’s acceptable to use unwrap
here. However, having a hardcoded, valid string doesn’t change the return type of the parse
method: we still get a Result
value, and the compiler will still make us handle the Result
as if the Err
variant is a possibility because the compiler isn’t smart enough to see that this string is always a valid IP address. If the IP address string came from a user rather than being hardcoded into the program and therefore did have a possibility of failure, we’d definitely want to handle the Result
in a more robust way instead.
So if you know that an error is never going to happen, but the compiler wants you to handle errors, unwrap
is cleaner and shorter than needing to provide a custom and unnecessary error message if you used expect
.