From ded09b6a3e3ba94550a8b55eed8f1ac330019ecf Mon Sep 17 00:00:00 2001 From: jRimbault Date: Mon, 29 Mar 2021 12:47:38 +0200 Subject: [PATCH 1/4] add fallible closure --- SUMMARY.md | 1 + idioms/fallible-closure.md | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 idioms/fallible-closure.md diff --git a/SUMMARY.md b/SUMMARY.md index 696c5cac..89adcf9c 100644 --- a/SUMMARY.md +++ b/SUMMARY.md @@ -19,6 +19,7 @@ - [Privacy For Extensibility](./idioms/priv-extend.md) - [Easy doc initialization](./idioms/rustdoc-init.md) - [Temporary mutability](./idioms/temporary-mutability.md) + - [Fallible closure](./idioms/fallible-closure.md) - [Design Patterns](./patterns/index.md) - [Behavioural](./patterns/behavioural/intro.md) diff --git a/idioms/fallible-closure.md b/idioms/fallible-closure.md new file mode 100644 index 00000000..797346d6 --- /dev/null +++ b/idioms/fallible-closure.md @@ -0,0 +1,36 @@ +# 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()?); + inner().unwrap_or("untitled") +} +``` + +## Motivation + +You may sometimes want to use the [`?`] operator inside an infallible function, +in those case a simple closure inside your function could be the simple solution. + +## Advantages + +This allows using the terser syntax permitted by the [`?`] operator inside an +infallible context. + +## Disadvantages + +This is hiding failure states from the consumer of the API. + + +[`?`]: https://doc.rust-lang.org/reference/expressions/operator-expr.html#the-question-mark-operator From 7c3191c901f3bd7114e9e623a1e8b03c953c34be Mon Sep 17 00:00:00 2001 From: jRimbault Date: Mon, 29 Mar 2021 13:00:47 +0200 Subject: [PATCH 2/4] remove line --- idioms/fallible-closure.md | 1 - 1 file changed, 1 deletion(-) diff --git a/idioms/fallible-closure.md b/idioms/fallible-closure.md index 797346d6..2a7f5603 100644 --- a/idioms/fallible-closure.md +++ b/idioms/fallible-closure.md @@ -32,5 +32,4 @@ infallible context. This is hiding failure states from the consumer of the API. - [`?`]: https://doc.rust-lang.org/reference/expressions/operator-expr.html#the-question-mark-operator From 94a160c1fb9c066d099dbc86cb6b1cebf4c68e6d Mon Sep 17 00:00:00 2001 From: jRimbault Date: Mon, 29 Mar 2021 14:38:09 +0200 Subject: [PATCH 3/4] Update idioms/fallible-closure.md tone was a bit condescending Co-authored-by: simonsan <14062932+simonsan@users.noreply.github.com> --- idioms/fallible-closure.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/idioms/fallible-closure.md b/idioms/fallible-closure.md index 2a7f5603..9bb89ba0 100644 --- a/idioms/fallible-closure.md +++ b/idioms/fallible-closure.md @@ -21,7 +21,7 @@ fn file_stem(path: &Path) -> &str { ## Motivation You may sometimes want to use the [`?`] operator inside an infallible function, -in those case a simple closure inside your function could be the simple solution. +in those case a closure inside your function could be the simple solution. ## Advantages From e37c7cba9d13424db96501bfc2c8a98ed7e826eb Mon Sep 17 00:00:00 2001 From: jRimbault Date: Tue, 30 Mar 2021 23:05:04 +0200 Subject: [PATCH 4/4] Update idioms/fallible-closure.md Co-authored-by: simonsan <14062932+simonsan@users.noreply.github.com> --- idioms/fallible-closure.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/idioms/fallible-closure.md b/idioms/fallible-closure.md index 9bb89ba0..c427725b 100644 --- a/idioms/fallible-closure.md +++ b/idioms/fallible-closure.md @@ -21,7 +21,7 @@ fn file_stem(path: &Path) -> &str { ## Motivation You may sometimes want to use the [`?`] operator inside an infallible function, -in those case a closure inside your function could be the simple solution. +in those cases a closure inside your function could be the simple solution. ## Advantages