Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Analysis of smol usage cases in Rust

2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

In this article, the editor introduces in detail the "case analysis of the use of smol in Rust", with detailed contents, clear steps and proper handling of details. I hope that this article "case analysis of the use of smol in Rust" can help you solve your doubts.

Brief introduction

Smol is a lightweight and efficient asynchronous runtime. It extends the standard library, and the entire runtime has only about 1500 lines of code. Author stjepang is the author of the famous crossbeam. His previous experience and thinking in the development of tokio and async-std produced the library smol, which was built from scratch. In fact, on the premise of achieving similar performance to tokio and async-std, smol code is short-term, completely independent of mio libraries, API is simpler, and there is no unsafe code! Moreover, it is compatible with tokio and async-std. Let's look at a simple example Tcp server side and client side is first an echo server side use std::net:: {TcpListener, TcpStream}

Use futures::io;use smol:: {Async, Task}

/ / return the input stream async fn echo (stream: Async)-> io::Result {io::copy (& stream, & mut & stream) .await?; Ok (())} intact

Fn main ()-> io::Result {smol::run (async {/ / create listener let listener = Async::::bind ("127.0.0.1 listener let listener 7000")?; println! ("Listening on {}", listener.get_ref (). Local_addr ()?); println! ("Now start a TCP client.")

/ / accept client request loop {let (stream, peer_addr) = listener.accept () .await?; println! ("Accepted client: {}", peer_addr)

/ / start a task response client Task::spawn (echo (stream)) .unwrap () .detach ()}} and then a client

Use std::net::TcpStream

Use futures::io;use futures::prelude::*;use smol::Async

Fn main ()-> io::Result {smol::run (async {/ / wraps asynchronous standard input / output let stdin = smol::reader (std::io::stdin ()); let mut stdout = smol::writer (std::io::stdout ()

/ / Connect server-side let stream = Async::::connect ("127.0.0.1 println 7000") .await?; println! ("Connected to {}", stream.get_ref (). Peer_addr ()?); println! ("Type a message and hit enter!\ n")

/ / stdin-> server; server returns-> stdout future::try_join (io::copy (stdin, & mut & stream), io::copy (& stream, & mut stdout),) .await?

Ok (())})} compatibility smol has very good compatibility, such as compatibility with tokio and async-std

Use std::time:: {Duration, Instant}

Use anyhow:: {Error, Result}

Fn main ()-> Result {smol::run (async {/ / sleep let start using async-std = Instant::now (); println! ("Sleeping using async-std..."); async_std::task::sleep (Duration::from_secs (1)) .await; println! ("Woke up after {:}", start.elapsed ())

/ / sleep let start using tokio = Instant::now (); println! ("Sleeping using tokio..."); tokio::time::delay_for (Duration::from_secs (1)) .await; println! ("Woke up after {:?}", start.elapsed ())

Ok (()})}

Note that tokio compatibility requires the following feature [dependencies] smol = {version = "0.1", features = [" tokio02 "]} to be enabled

After reading this, the article "smol usage case Analysis in Rust" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report