Skip to content

Instantly share code, notes, and snippets.

@stepancheg
Last active May 11, 2019 22:45
Show Gist options
  • Save stepancheg/bcb68597fca8a5bf12edc71afdea141e to your computer and use it in GitHub Desktop.
Save stepancheg/bcb68597fca8a5bf12edc71afdea141e to your computer and use it in GitHub Desktop.
In defence of the prefix await (and the prefix await?)

In defence of the prefix await (and the prefix await?)

TL;DR: prefix await is better because it is consistent with the rest of the language despite of its drawbacks.

There's ongoing discussion whether prefix or postfix await expression should be added to Rust.

let response = connection.request().await; // posfix
let response = await connection.request(); // prefix

Arguments for a prefix or postfix await are:

  • error handing is easier with postfix await, but it easy enough with await? keyword and map, map_err and and_then combinator
  • learnability is not a big issue, although prefix await is what people used to work with C#, Python, JavaScript and other languages, any syntax is easy to learn compared to underlying semantic, so it's not really an issue
  • chaining is convenient, but rarely needed and can be solved with braces or introducing local variables

I consider all these arguments less important than aesthetics.

One great feature of Rust is its internal consistency.

Internal consistency

Rust language might look unusual to people coming from other languages (struct has no inheritance, pointers to traits cannot be downcast and int is called isize).

But when people learn Rust well enough, they start to appreciate not just Rust power and expressiveness, but also the beautiful design of the language. Despite all it's power Rust is simple and consistent.

One of the properties that make Rust syntax consistent is a style where most of the syntax rules start with a keyword.

Variable declarations begin with let, structs begin with struct and loop syntax is a loop or for keyword followed by loop condition and body.

if is a prefix keyword even if people might want to write it in Python-like style:

let abs = x if x >= 0 else -x;

match is a prefix keyword even if it might be convenient to have it postfix:

self.state.compare_exchange(0, 2).match {
  Ok(_) => return,
  Err(_) => continue,	
}

Rust doesn't have a special magic syntax of invoking single argument constructor, although it could be convenient to write it like:

return result.code() |> Ok;

It's worth choosing prefix await for the sake of keeping Rust the language we love even if it leads to a slightly higher number of LOC or slightly harder to type await expressions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment