New syntax for async/await in Rust
Date:
[]
Categories:
[Technical]
Tags:
[Rust]
Rust
I did not (yet) described Rust in my blog. So to be quick, Rust is a programming language (and no I will never talk about the game with the same name.) which aim at providing high level ecosystem and functionalities to low level languages. That is if you come from a low level language background. If you come from a scripting/high level language Rust aim at bringing you low level execution speed while keeping you in your usual ecosystem (npm, pip, …).
My experience
After this quick introduction to Rust, I should briefly point out that I'm learning Rust "for fun" since last summer by reading "the book". I'm creating a crate that should be close to it's publication… when I find some motivation to do so 😅.
I have glanced over the uses of async/await
in some languages but never used it myself. So I will not pretend to have a strong opinion on the proposed syntax by the lang team, I use it as a starting point.
The proposed await syntax
So, to come back to this post's subject: the Rust lang team proposed to introduce a new syntax to use the Future await
keyword. It is the dot await
syntax as they call it.
Even if I'm more used to prefix syntax (match {}
, if {}
, …) than the postfix one, I don't think I'm competent to judge this debate.
My problem with it
I don't think the dot keyword
syntax is perfect as is. Sure if you compare it to the method syntax it's like a parenthesis less version, and why not. But on the other hand it's exactly the property syntax. If a function return a struct: fn get_foo -> struct
, I may want to use only one property of it like get_foo().bar
… and that look too much like the dot await
notation. It may not be the idiomatic use, but I know that I'm doing it when hacking some JS.
My proposition
When reading I thought of commas await
: foo(),await
but it may not be practical nor doable because commas are already separating arguments, even without space.
I like the idea of reserving the !
for magic elements. Anytime you want to do some ways but need the help of some (compiler?) magic:
- You want a function with a bit of magic ? We already have
macro!()
. - You want to modify the comportment of functions ? We are discussing about
!modifiers
or more generally!keywords
. - You need to do some magic with values ? We may have
.keyMethods!()
or!keyMethods()
some days.
By the way please, do not create !match
or any normal control flow with this syntax. Keep this syntax exclusively without argument. You want to introduce !await
as a keyword because we can't create this functionality as a function/macro/method. Then stay true to your words: a keyword cannot have parameters. Maybe a !loop
for infinite loop on a function on even !panic
or !unwrap
if we need it for… something. I'm not sure I really want that either.
If you want that one of the next keywords to have parameters, then I would prefer you go to one of my proposed method like syntax and put the arguments in the parenthesis. This way the postfix keywords enforced by the compiler keep a consistent syntax with or without parameters.
Comments
To prevent you from creating an account just to comment on my blog, I invite you to comment this post on its Fediverse announcement, Reddit /r/rust.
PS: Syndication
You can subscribe to the RSS, Atom, or JSON flux.