-
Notifications
You must be signed in to change notification settings - Fork 374
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 fallible closure to idioms #250
base: main
Are you sure you want to change the base?
Changes from all commits
ded09b6
7c3191c
94a160c
1390e0c
e37c7cb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Fallible closure inside infallible function | ||
|
||
## Description | ||
|
||
Build a fallible closure inside of an apparently infallible function. | ||
|
||
## Example | ||
|
||
```rust | ||
use std::path::Path; | ||
|
||
// this function hides its failure states | ||
fn file_stem(path: &Path) -> &str { | ||
// this closure is fallible, so it can make use of `?` | ||
// fn() -> Option<&str> | ||
let inner = || Some(path.file_stem()?.to_str()?); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe this example is not making full use of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It could be written : let inner = || path.file_stem()?.to_str(); but that wouldn't show much There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I do think that is better. I don't know why we need to have another layer for that. |
||
inner().unwrap_or("untitled") | ||
} | ||
``` | ||
|
||
## Motivation | ||
|
||
You may sometimes want to use the [`?`] operator inside an infallible function, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
in those cases a closure inside your function could be the simple solution. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why is this a good and simple solution in these cases, an explanation for it would be good There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this even an idiom? I thought it is just a syntax? |
||
|
||
## Advantages | ||
|
||
This allows using the terser syntax permitted by the [`?`] operator inside an | ||
infallible context. | ||
Comment on lines
+28
to
+29
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bit repetitive, |
||
|
||
## Disadvantages | ||
|
||
This is hiding failure states from the consumer of the API. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why is that bad and when do you really want to avoid it? mentioning some cases would be good and probably also state, that if this is overused it could be considered an |
||
|
||
[`?`]: https://doc.rust-lang.org/reference/expressions/operator-expr.html#the-question-mark-operator |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We might find a better headline for the summary to let this idiom be found more easily.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, this often comes up when people want to avoid "arrow code" with let guards or "match hell", maybe something around that ?