-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add the Entry API to Option<T> #1278
Comments
@wesleywiser I think you may be mis-interpretting the error messages? None of your examples (including the "working" solution) will compile as-is, but with a couple of modifications they all work fine: http://is.gd/Ru5Akw Adding an entry-style API for Option is unnecessary: you can just add useful methods directly on Option itself. For example, it already has a |
Literally all the entry API does is save on the cost of a double lookup, which is Not A Thing for Option |
@Diggsey Interesting... Yes, you're right I misunderstood the error. Thanks for the explanation! I like the idea of an |
Motivation
While working on a project recently, I needed to optimize some code by performing an expensive operation lazily. The change looked something like this:
Before:
After:
Initially, I tried to write
get_image_stats
like this:But I ran into borrow errors because the
match
had an outstanding borrow when I tried to assign toself.image_stats
. After thinking about it and researching similar issues on StackOverflow, I came up with the above code which works. Thinking about it some more lead me to realize that this is similar to the Entry API. If there was an API like that forOption
, then the implementation ofget_image_stats
could be written like this:I think this is a lot nicer than either of my tries.
Implementation
I pretty shamelessly ripped this off of the existing Entry API. There's probably a better way to write some of this; I'm still getting the hang of Rust.
The text was updated successfully, but these errors were encountered: