-
Notifications
You must be signed in to change notification settings - Fork 55
Async Main #42
Comments
Also relevant is that C# 7 has a similar This could be useful to other Rust tools which also need to use |
You do not need to move your code from main. You can put your async code inside an |
@rushmorem My question is actually specifically about being able to use My suggestion/proposal goes hand in hand with the already being implemented RFC that allows returning a result from main. :) |
We could perhaps try to allow this! I fear though that we don't have enough of a "default" runtime to know how to run the future returned by |
The way I was imagining it was that code like this: #[async]
fn foo() -> Result<(), i32> {
println!("{}", 1 + await!(bar())?);
Ok(())
}
#[async]
fn bar() -> Result<i32, i32> {
Ok(2)
}
fn main() -> Result<(), i32> {
foo().wait()
} Would become like this: #[async]
fn main() -> Result<(), i32> {
println!("{}", 1 + await!(bar())?);
Ok(())
}
#[async]
fn bar() -> Result<i32, i32> {
Ok(2)
} I think that this fits really well with the RFC that allows returning Result from main. Is there more to that than just calling |
@sunjay ah yeah unfortunately |
@alexcrichton Ah yes you're right. Is there any default behaviour we could support at all? I imagine that there must be simpler cases which don't need the entire main loop in order to function. Are there any? |
Well yes my point is we could support something like |
Hi! Please let me know if this isn't the right place to ask about this or if it has already been discussed. :)
I wrote a library to create animated drawings in Rust. It's specifically designed to be a great tool for teaching Rust. One of the things I'd like to add a few months from now is a whole set of lessons about Rust and async IO. I plan to refactor the library to return futures and then teach the different future combinators using the drawing methods in the library. It should be really cool!
One of the things users of my library will do a lot is write code directly in the main function. For example, to draw a heart, their code might look something like this:
Each of these method calls directs a "turtle" (the pen used for drawing) to perform a certain action.
If I were to start to port this to use futures, I would need to move the entire contents of
main
into a new function, and replacemain
with a call towait()
. That's a lot of code to suddenly have to move. I personally understand why I need to do that because I know the mechanics behind it, but for a newer Rust user, I think it's important to remove that friction entirely. If they're able to use async/await syntax everywhere else, I think they would naturally try to use it inmain
too.This is a very similar issue to the accepted RFC for allowing
?
inmain
. For the same reason that having?
inmain
is convenient and important, I think having async and await in main from the beginning is really critical too. It would be great if we could have that functionality from the beginning when async/await is added to the language.The text was updated successfully, but these errors were encountered: