3

I'm researching and playing with Rust's async/.await to write a service in Rust that will pull from some websockets and do something with that data. A colleague of mine (who did this similar "data feed importing" in C#) has told me to handle these feeds asynchronously, since threads would be bad performance-wise.

It's my understanding that, to do any async in Rust, you need a runtime (e.g. Tokio). After inspecting most code I've found on the subject it seems that a prerequisite is to have a:

#[tokio::main]
async fn main() {
    // ...
}

which provides the necessary runtime which manages our async code. I came to this conclusion because you cannot use .await in scopes which are not async functions or blocks.

This leads me to my main question: if intending to use async in Rust, do you always needs an async fn main() as described above? If so, how do you structure your synchronous code? Can structs have async methods and functions implemented (or should they even)?

All of this stems from my initial approach to writing this service, because the way I envisioned it is to have some sort of struct which would handle multiple websocket feeds and if they need to be done asynchronously, then by this logic, that struct would have to have async logic in it.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
monolith937
  • 429
  • 1
  • 4
  • 13
  • I edited the original question. Should I have posted a new one ? – monolith937 Jun 16 '20 at 11:19
  • 1
    It's hard to answer multiple questions made in one post. Please separate them into multiple questions so that we can help you better and so that your questions will help others in the future that have one of the same questions as you! – Shepmaster Jun 16 '20 at 14:26
  • It looks like your question might be answered by the answers of [How do I synchronously return a value calculated in an asynchronous Future in stable Rust?](https://stackoverflow.com/q/52521201/155423). If not, please **[edit]** your question to explain the differences. Otherwise, we can mark this question as already answered. – Shepmaster Jun 16 '20 at 14:26
  • *how do you structure your synchronous code* — this is opinion based and not a good question for Stack Overflow. – Shepmaster Jun 16 '20 at 14:27
  • *Can structs have async methods and functions implemented* — yes. This shows that you haven't tried what you are asking. – Shepmaster Jun 16 '20 at 14:27
  • *should they even* — this is opinion based and not a good question for Stack Overflow. – Shepmaster Jun 16 '20 at 14:28
  • Because this question in its current form appears off-topic, you may wish to look at other resources. More open-ended questions and discussions are welcome on [the Rust users forum](https://users.rust-lang.org/), [the Rust subreddit](https://www.reddit.com/r/rust/), [the Rust Discord server](https://www.rust-lang.org/community), or [the Stack Overflow Rust chat](https://chat.stackoverflow.com/rooms/62927/rust). – Shepmaster Jun 16 '20 at 14:28
  • Does this answer your question? [How do I synchronously return a value calculated in an asynchronous Future in stable Rust?](https://stackoverflow.com/questions/52521201/how-do-i-synchronously-return-a-value-calculated-in-an-asynchronous-future-in-st) – E_net4 Jun 16 '20 at 14:34
  • Specifically: *on any function (not just `main`!) to convert it from an asynchronous function to a synchronous one* – Shepmaster Jun 16 '20 at 14:42
  • My apologies in advance, it seems I have quite the homework to do. I guess I'm still in the 'design' phase of my code (which falls uneasy on me) and the search results have not been that graceful. I will come up with a better (new) and more concrete question once I exhaust my research. Please feel free to close this question – monolith937 Jun 16 '20 at 15:41

1 Answers1

6

No. The #[tokio::main] is just a convenience feature which you can use to create a Tokio runtime and launch the main function inside it.

If you want to explicitly initialize a runtime instance, you can use the Builder. The runtime has the spawn method which takes an async closure and executes it inside the runtime without being async itself. This allows you to create a Tokio runtime anywhere in your non-async code.

Kotauskas
  • 1,239
  • 11
  • 31