You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This RFC proposes promoting the use of match statements by supporting postfix-match, reducing the use of some of these methods terse and potentially confusing method chains.
@@ -46,9 +46,9 @@ variant. Some powerful techniques like pattern binding, nested patterns and or p
46
46
allow for some versatile and concise, but still fairly readable syntax for dealing
47
47
with these types.
48
48
49
-
Rust often features functional approachs to lots of problems. For instance,
49
+
Rust often features functional approaches to lots of problems. For instance,
50
50
it's very common to have chains of `map`, `and_then`, `ok_or`, `unwrap`
51
-
to process some `Option` or `Result` type in a pipeline, rather than continously reassigning to new variable bindings.
51
+
to process some `Option` or `Result` type in a pipeline, rather than continuously reassigning to new variable bindings.
52
52
53
53
```rust
54
54
letx=Some(42);
@@ -59,7 +59,7 @@ let magic_number = x.map(|x| x * 5)
59
59
```
60
60
61
61
Some of these provided method chains are fairly readable, like the ones presented above,
62
-
but sometimes the desire to use long method chains is met with unweildy hacks or awkward function arguments.
62
+
but sometimes the desire to use long method chains is met with unwieldy hacks or awkward function arguments.
63
63
64
64
```rust
65
65
letx=Some("crustaceans");
@@ -201,7 +201,7 @@ context.client
201
201
.await
202
202
.match {
203
203
Err(_) =>Ok("Ferris"),
204
-
Ok(resp) =>resp.json::<Option<String>>().await,
204
+
Ok(resp) =>resp.json::<String>().await,
205
205
// this works in a postfix-match ^^^^^^
206
206
}
207
207
```
@@ -254,12 +254,12 @@ make the language more flexible such that match statements aren't a hindrance.
254
254
255
255
### Postfix Macros
256
256
257
-
[postfix macros](https://github.com/rust-lang/rfcs/pull/2442) have been an idea for many years now. If they were to land, this feature
258
-
could easily be implemented as a macro (bikeshedding on postfix macro syntax):
257
+
[postfix macros](https://github.com/rust-lang/rfcs/pull/2442) have been an idea for many years now.
258
+
If they were to land, this feature could easily be implemented as a macro:
259
259
260
260
```rust
261
261
macromatch_! (
262
-
postfix {$(
262
+
{ $self:expr;$(
263
263
$arm:pat=>$body:expr,
264
264
)+ } => {
265
265
match$self { $(
@@ -275,10 +275,19 @@ still not close to agreeing on syntax or behaviour.
275
275
### Pipes
276
276
277
277
I've already mentioned [tap](#tap-pipelines) for how we can do prefix-in-postfix,
278
-
so we could promote the use of `.pipe` instead. However, using the pipe method makes
278
+
so we could promote the use of `.pipe()` instead. However, using the pipe method makes
279
279
async awkward and control flow impossible.
280
280
281
-
Alternatively we could have a new builtin pipeline operator
281
+
```rust
282
+
x.pipe(|x|async {
283
+
matchx {
284
+
Err(_) =>Ok("Ferris"),
285
+
Ok(resp) =>resp.json::<String>().await,
286
+
}
287
+
}).await
288
+
```
289
+
290
+
We could add a new builtin pipeline operator
282
291
(e.g. `|>`<sup>[0]</sup> or `.let x {}`><sup>[1]</sup>)
283
292
but this is brand new syntax and requires a whole new set of discussions.
284
293
@@ -304,7 +313,7 @@ I've heard many accounts from people that postfix-await is one of their favourit
304
313
305
314
Method call chains will not lifetime extend their arguments. Match statements, however,
306
315
are notorious for having lifetime extension. It is currently unclear if promoting these
307
-
usecases of match would cause more [subtle bugs](https://fasterthanli.me/articles/a-rust-match-made-in-hell#my-actual-bug), or if it's negligable
316
+
use-cases of match would cause more [subtle bugs](https://fasterthanli.me/articles/a-rust-match-made-in-hell#my-actual-bug), or if it's negligible
0 commit comments