0
点赞
收藏
分享

微信扫一扫

【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


社区学习交流平台订阅:

  • Rustcc 论坛:支持 rss
  • 微信公众号:Rust 语言中文社区


举报

相关推荐

0 条评论