英文:
tokio-tungstenite with tokio::select! macro?
问题
以下是您要翻译的内容:
"The documentation for the tokio-tungstenite crate mostly just directs you to the examples section.
The client example hasn't been updated in a few years and I saw some unfamiliar async code here and because Rust's async APIs are still in flux I wanted to check the use futures_util::future::select
was still idiomatic in this situation.
In particular, it seems to work fine if I replace it with the more commonly seen tokio::select!
macro, which doesn't require pinning the passed futures.
I have good familiarity with tokio's APIs but not so much the lower level futures
ones. Is there a reason to manually pin here and use the future::select
instead? More generally, in modern idiomatic async Rust code when would one use the latter?
英文:
The documentation for the tokio-tungstenite crate mostly just directs you to the examples section.
The client example hasn't been updated in a few years and I saw some unfamiliar async code here and because Rust's async APIs are still in flux I wanted to check the use futures_util::future::select
was still idiomatic in this situation.
In particular, it seems to work fine if I replace it with the more commonly seen tokio::select!
macro, which doesn't require pinning the passed futures.
I have good familiarity with tokio's APIs but not so much the lower level futures
ones. Is there a reason to manually pin here and use the future::select
instead? More generally, in modern idiomatic async Rust code when would one use the latter?
答案1
得分: 1
可以用tokio::select!
(或futures::select!
)来替换任何使用futures::future::select()
的地方,它们都执行相同的操作。select!
允许选择多个未来,而select()
则返回一个具名的未来。我倾向于在可以的情况下使用select!
(我认为它可能也更高效,但我不太确定)。
英文:
It should be fine to replace any use of futures::future::select()
with tokio::select!
(or futures::select!
). They both do the same thing. select!
allow for more than one future, while select()
gives you a named future. I prefer select!
when I can (I think it is also more performant, but I'm not sure).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论