5
submitted 1 year ago by xhci@lemmy.world to c/rustlang@lemmyrs.org

I’m just starting to learn Rust, and was wondering if there is a good way to do coroutines/channels. I’ve grown fond of languages based around CSP, but am not sure that maps well to idiomatic Rust.

Thanks in advance!

all 7 comments
sorted by: hot top controversial new old
[-] yellowflame8818@lemmy.world 3 points 1 year ago

You can use mpsc for channels, there's oneshots too

[-] dillekant@slrpnk.net 3 points 1 year ago

Wait is there a difference between coroutines and async/await? I thought they were practically the same model.

[-] chrysn@chaos.social 1 points 1 year ago

Coroutines are one means of implementing async; the way they are implemented in Rust is more like building a state machine out of the async function. It can still be mapped to coroutines, and there are probably crates that use async and macros to make coroutines usable on stable, but the effort to have a stable language feature is still ongoing, with https://lang-team.rust-lang.org/design_notes/general_coroutines.html giving the overview.

[-] thomask@lemmy.sdf.org 2 points 1 year ago* (last edited 1 year ago)

One approach I'm personally fond of is actor-style async programming with tokio. It's easy to mingle timers with receiving messages from channels, which is quite powerful. This post by Alice Rhyl is a good description of the idea: https://ryhl.io/blog/actors-with-tokio/

[-] roign@lemmy.world 1 points 1 year ago* (last edited 1 year ago)

This.

tokio tasks are green threads. https://docs.rs/tokio/latest/tokio/task/

tokio also offers a variety of channels for synchronization between tasks. https://docs.rs/tokio/latest/tokio/sync/index.html

[-] netwren@lemmy.world 1 points 1 year ago

Super interested to see if this is true as well. AFAIK Rust focuses on threading and async/await with Tokio being the main focus.

I believe there is some support of green threads with Tokio configuration but I'm not 100% sure and am just writing off the top of my head because nobody has responded yet.

this post was submitted on 06 Jul 2023
5 points (100.0% liked)

Rust Lang

3 readers
1 users here now

Rules [Developing]

Observe our code of conduct

Constructive criticism only

No endless relitigation

No low-effort content

No memes or image macros

No NSFW Content

founded 1 year ago
MODERATORS