Skip to content

常用的Crate

https://docs.rs/

常用

async-std
futures
serde_json
serde_yaml
http_types
isahc
warp
http_types
surf
bytes
crossbeam
async_tungstenite
tungstenite
log
http
lz4
rand
url
webpki
redis
clap

serde

https://github.com/serde-rs/serde
https://docs.serde.rs/serde/

chrono

https://github.com/chronotope/chrono/

处理日期时间

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
fn main() {
    use chrono::prelude::*;
    use std::{thread, time};

    let mut local: DateTime<Local> = Local::now();
    println!("{}", local);
    let five_seconds = time::Duration::new(5, 0);
    thread::sleep(five_seconds);
    local = Local::now();
    println!("{}", local);
}
// 2020-02-24 10:53:57.855642 +08:00
// 2020-02-24 10:54:02.857297 +08:00

注意尽量使用NaiveDateTimeNaiveDateNaiveTime,这样效率会高很多

使用Date<Local>DateTime<Local>会非常慢

可以Local::now().naive_local()DateTime<Local>转换成NaiveDateTime

rust-ini

https://github.com/zonyitoo/rust-ini

https://docs.rs/rust-ini/0.15.2/ini/

lazy-static

https://github.com/rust-lang-nursery/lazy-static.rs

tokio

https://docs.rs/tokio/0.2.11/tokio/index.html

https://github.com/tokio-rs/tokio

https://tokio.rs/

Rust 中的异步编程框架,它将复杂的异步编程抽象为 Futures、Tasks 和 Executor,并提供了 Timer 等基础设施

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
use chrono::prelude::*;
use std::{thread, time};
use tokio::io::{self, AsyncReadExt};
use std::error::Error;
use std::time::Duration;
use tokio::time::delay_for;
use tokio::task;

#[tokio::main]
async fn main() {
    let mut local: DateTime<Local> = Local::now();
    println!("{}", local);
    let five_seconds = time::Duration::new(5, 0);
    // delay_for(Duration::from_secs(2)).await;
    // delay_for(Duration::from_secs(2)).await;

    let join = task::spawn(async {
        // ...
        delay_for(Duration::from_secs(2)).await;
        println!("hello world!");
    });
    let join2 = task::spawn(async {
        // ...
        delay_for(Duration::from_secs(2)).await;
        println!("hello world!2");
    });
    delay_for(Duration::from_secs(2)).await;
    join.await;
    join2.await;
    local = Local::now();
    println!("{}", local);
}
// 2020-02-24 12:50:03.023939 +08:00
// hello world!
// hello world!2
// 2020-02-24 12:50:05.025403 +08:00

websocket

https://docs.rs/websocket/0.24.0/websocket/index.html

https://github.com/websockets-rs/rust-websocket

1
2
3
4
[dependencies]
chrono = { version = "0.4", features = ["serde"] }
tokio = { version = "0.2", features = ["full"] }
websocket = "0.24.0"
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
use chrono::prelude::*;
use std::{thread, time};
use tokio::io::{self, AsyncReadExt};
use std::error::Error;
use std::time::Duration;
use tokio::time::delay_for;
use tokio::task;
use websocket::ClientBuilder;
use websocket::OwnedMessage;

#[tokio::main]
async fn main() {
    let mut local: DateTime<Local> = Local::now();
    println!("{}", local);
    let five_seconds = time::Duration::new(5, 0);
    // delay_for(Duration::from_secs(2)).await;
    // delay_for(Duration::from_secs(2)).await;

    let join = task::spawn(async {
        // ...
        delay_for(Duration::from_secs(2)).await;
        println!("hello world!");
    });
    let join2 = task::spawn(async {
        // ...
        delay_for(Duration::from_secs(2)).await;
        println!("hello world!2");
    });
    let join3 = task::spawn(async {
        // ...
        let mut client = ClientBuilder::new("ws://hq.sinajs.cn/wskt?list=s_sh000001").unwrap()
                        .connect(None).unwrap();
        for message in client.incoming_messages() {
            match message {
                Ok(s) =>  if s.is_data() {
                    println!("{:?}", s);
                },
                Err(e) => continue,
            }

        }
        println!("hello world!3");
    });
    join3.await;
    join.await;
    join2.await;
    local = Local::now();
    println!("{}", local);
}