When should we use unwrap vs expect in Rust

Rust doesn’t have function overloading, so there should be a way to declare “unwrap with a message”, and that is expect.

  1. expect == unwrap with a message
  2. expect_err == unwrap_err with a message

About usage scenarios of “unwrap vs expect” Rust Book (Ch 9) says:

Using expect instead of unwrap and providing good error messages can convey your intent and make tracking down the source of a panic easier.
Because this error message starts with the text we specified… it will be easier to find where in the code this error message is coming from.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)