【Rust 日报】2021-06-06 TheAlgorithms Rust 实现

TheAlgorithms: Rust

大名鼎鼎的 The Algorithms 的 Rust 版本,使用 Rust 实现所有算法。

GitHub 地址:TheAlgorithms/Rust: All Algorithms implemented in Rust

Rustpad:开源协作文本编辑器

  • 高效且最小的协作代码编辑器、自托管、无需数据库

  • 使用 warp web服务框架和 operational-transform 库

  • 使用 wasm-bindgen 将文本操作逻辑编译为在浏览器运行的 WebAssembly 代码

  • 客户端通过 WebSocket 与存储内存数据结构的中央 server 通信

GitHub 地址:ekzhang/rustpad: Efficient and minimal collaborative code editor, self-hosted, no database required

textwrap:一个用于包装和缩进文本的库

fn main() {
    let text = "textwrap: an efficient and powerful library for wrapping text.";
    println!("{}", textwrap::fill(text, 28));
}

// output
textwrap: an efficient
and powerful library for
wrapping text.

GitHub 地址:mgeisler/textwrap: An efficient and powerful Rust library for word wrapping text.

linya:轻量并发进度条

GitHub 地址:fosskers/linya: Simple concurrent progress bars.

youchoose:一个简单易用的 Rust 命令行菜单

GitHub 地址:nathom/youchoose: A lightweight terminal menu for Rust

使用 Tokio 创建简单聊天服务器

仅使用 tokio 实现的简单聊天服务器:

use tokio::{
    io::{AsyncBufReadExt, AsyncWriteExt, BufReader},
    net::TcpListener,
    sync::broadcast,
};

#[tokio::main]
async fn main() {
    let listener = TcpListener::bind("localhost:8080").await.unwrap();
    let (tx, _rx) = broadcast::channel(10);
    loop {
        let (mut socket, addr) = listener.accept().await.unwrap();
        let tx = tx.clone();
        let mut rx = tx.subscribe();
        tokio::spawn(async move {
            let (reader, mut writer) = socket.split();
            let mut reader = BufReader::new(reader);
            let mut line = String::new();
            loop {
                tokio::select! {
                    result = reader.read_line(&mut line) => {
                        if result.unwrap() == 0 {
                            break;
                        }
                        tx.send((line.clone(), addr)).unwrap();
                        line.clear();
                    }
                    result = rx.recv() => {
                        let (msg, other_addr) = result.unwrap();
                        if addr != other_addr {
                            writer.write_all(msg.as_bytes()).await.unwrap();
                        }
                    }
                }
            }
        });
    }
}

YouTube 链接:(19) Creating a Chat Server with async Rust and Tokio - YouTube


From 日报小组 长琴

社区学习交流平台订阅:

  • Rustcc 论坛:支持 rss

  • 微信公众号:Rust 语言中文社区

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值