
rust第六篇,用rust写的通用工具
just 为您提供一种保存和运行项目特有命令的便捷方式。
命令,在此也称为配方,存储在一个名为 justfile 的文件中,其语法受 make 启发:
然后你可以用 just RECIPE 运行它们
$ just test-all
cc *.c -o main
./test --all
Yay, all your tests passed!just 有很多很棒的特性,而且相比 make 有很多改进:
just 是一个命令运行器,而不是一个构建系统,所以它避免了许多 make 的复杂性和特异性。不需要 .PHONY 配方!sh,你需要 选择一个不同的 Shell)。just 可以 加载.env文件,简化环境变量注入。just 可以从任何子目录中调用,而不仅仅是包含 justfile 的目录。https://suibianxiedianer.github.io/rust-cli-book-zh_CN/tutorial/cli-args_zh.html
支持在其他语言中无缝接入Rust中的类型, 支持语言包括typescript、kotlin、swift、scala、go
在Cargo.toml中加入依赖
typeshare = "1.0.0"rust中的类型
// Rust type definitions
#[typeshare]
struct MyStruct {
my_name: String,
my_age: u32,
}
#[typeshare]
#[serde(tag = "type", content = "content")]
enum MyEnum {
MyVariant(bool),
MyOtherVariant,
MyNumber(u32),
}生成的typescript类型
// Generated Typescript definitions
export interface MyStruct {
my_name: string;
my_age: number;
}
export type MyEnum =
| { type: "MyVariant", content: boolean }
| { type: "MyOtherVariant", content: undefined }
| { type: "MyNumber", content: number };webAssembly构建工具
https://github.com/extism/extism
https://github.com/firecracker-microvm/firecracker
查询
https://github.com/obi1kenobi/trustfall
diff高亮
https://github.com/dandavison/delta
https://github.com/ast-grep/ast-grep
解析markdown
https://github.com/kivikakk/comrak?tab=readme-ov-file
LLM代理
use rig::{completion::Prompt, providers::openai};
#[tokio::main]
async fn main() {
// Create OpenAI client and agent.
// This requires the `OPENAI_API_KEY` environment variable to be set.
let openai_client = openai::Client::from_env();
let gpt4 = openai_client.agent("gpt-4").build();
// Prompt the model and print its response
let response = gpt4
.prompt("Who are you?")
.await
.expect("Failed to prompt GPT-4");
println!("GPT-4: {response}");
}https://github.com/cloudflare/lol-html
使用
use lol_html::{element, HtmlRewriter, Settings};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut output = vec![];
let mut rewriter = HtmlRewriter::new(
Settings {
element_content_handlers: vec![
element!("a[href]", |el| {
let href = el
.get_attribute("href")
.expect("href was required")
.replace("http:", "https:");
el.set_attribute("href", &href)?;
Ok(())
})
],
..Settings::default()
},
|c: &[u8]| output.extend_from_slice(c)
);
rewriter.write(b"<div><a href=")?;
rewriter.write(b"http://example.com>")?;
rewriter.write(b"</a></div>")?;
rewriter.end()?;
assert_eq!(
String::from_utf8(output)?,
r#"<div><a href="https://example.com"></a></div>"#
);
Ok(())
}