Get the latest tech news

Don't unwrap options: There are better ways (2024)


I noticed that handling the None variant of Option without falling back on unwrap() is a common papercut in Rust. More specifically, the problem arises when you want to return early from a function that retur…

It has been discussed a million times already, but, surprisingly, not even the Rust book mentions my favorite approach to handling that, and many forum posts are outdated. But what if the final return type of your function has to be a Result or if you want to convey more information about the missing value to the caller? However, you might not be able to change get_user() to return a Result for various reasons, for example, if it’s part of a library or if it’s used in many places or if other callers don’t treat the absence of a user as an error.

Get the Android app

Or read this on Hacker News

Read more on:

Photo of Options

Options

Photo of better ways

better ways

Photo of unwrap

unwrap

Related news:

News photo

Players Have Too Many Options to Spend $80 on a Video Game

News photo

OpenAI’s Product Chief Says Users Need Better Ways to Share ChatGPT Posts

News photo

Forerunner’s long game: As startups stall before IPO, all options are on the table