重要前提
安装AI Skills的关键前提是:必须科学上网,且开启TUN模式,这一点至关重要,直接决定安装能否顺利完成,在此郑重提醒三遍:科学上网,科学上网,科学上网。查看完整安装教程 →
npx skills add https://github.com/modu-ai/moai-adk --skill moai-lang-rustRust 1.92+ 开发专家,精通构建高性能、内存安全应用程序的深层模式。
自动触发:.rs、Cargo.toml、async/await、Tokio、Axum、SQLx、serde、生命周期、特征
核心应用场景:
快速模式:
Axum REST API:使用路由宏将路径和处理程序链式组合来创建路由器。添加 with_state 以共享状态。使用 tokio::net 绑定 TcpListener,并使用 axum::serve 启动服务。
使用 SQLx 的异步处理程序:定义异步处理函数,接收用于 AppState 的 State 提取器和用于 id 的 Path 提取器。使用 sqlx::query_as! 宏配合 SQL 字符串和绑定参数。在连接池上调用 fetch_optional,等待结果,并使用 ok_or 进行错误转换。返回包装在 Json 中的结果。
现代 Rust 特性:
异步特征(稳定):使用 async fn 签名定义特征。为具体类型实现特征,并提供 async fn 实现。在特征方法中直接调用 sqlx 宏。
Let-Else 模式:使用 let Some(value) = option else { return ... } 进行提前退出。链式使用多个 let-else 语句进行顺序验证。在 else 块中返回错误类型。
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
安装:在 Cargo.toml 的 dependencies 部分,添加 axum 版本 0.8、tokio 版本 1.48(启用完整功能)和 tower-http 版本 0.6(启用 cors 和 trace 功能)。
完整 API 设置:从 axum::extract 导入提取器和路由宏。定义派生 Clone 的 AppState 结构体,其中包含 PgPool。在 tokio::main 异步主函数中,使用 PgPoolOptions 创建连接池,设置最大连接数,并使用环境变量中的 DATABASE_URL 进行连接。使用路径和处理程序的路由链构建路由器,添加 CorsLayer,并调用 with_state。绑定 TcpListener 并调用 axum::serve。
处理程序模式:定义异步处理函数,接收适当类型的 State、Path 和 Query 提取器。使用 sqlx::query_as! 进行类型安全的查询和位置绑定。返回包含 Json 成功结果和 AppError 失败结果的 Result。
任务生成和通道:创建具有容量的 mpsc 通道。使用 tokio::spawn 生成工作器任务,这些任务在循环中从通道接收消息。对于超时,使用 tokio::select! 宏,包含操作分支和睡眠分支,超时时返回错误。
类型安全查询:在结构体上派生 sqlx::FromRow 以进行自动映射。使用 query_as! 宏进行编译时检查的查询。在连接池上调用 fetch_one 或 fetch_optional。对于事务,调用 pool.begin,在事务引用上执行查询,然后调用 tx.commit。
在结构体上派生 Serialize 和 Deserialize。使用 serde 属性和 rename_all 进行大小写转换。使用 rename 属性进行字段特定命名。对 Option::is_none 使用 skip_serializing_if。使用 default 属性设置默认值。
thiserror:在枚举上派生 Error,并使用 error 属性设置显示消息。使用 from 属性实现从源错误的自动转换。通过匹配变体并返回状态码和包含错误消息的 Json 正文来实现 IntoResponse。
在主 Cli 结构体上派生 Parser,并使用 command 属性设置名称、版本和描述。使用 arg 属性设置全局标志。在枚举上派生 Subcommand 以定义子命令。在主函数中匹配命令以分派逻辑。
使用 cfg(test) 属性创建测试模块。定义 tokio::test 异步函数。调用设置辅助函数,调用被测函数,并使用 assert! 宏进行验证。
如需全面了解,包括:
请参阅:reference/engineering.md 了解所有权和特征,reference/performance.md 了解优化,reference/guidelines.md 了解编码标准
发布构建:在 Cargo.toml 的 profile.release 部分,启用 lto,将 codegen-units 设置为 1,将 panic 设置为 abort,并启用 strip。
最小化容器:使用多阶段 Dockerfile。第一阶段使用 rust alpine 镜像,复制 Cargo 文件,创建虚拟主文件用于依赖项缓存,构建发布版本,复制源代码,触摸 main.rs 以触发重建,构建最终发布版本。第二阶段使用 alpine,从构建器复制二进制文件,暴露端口,并设置 CMD。
速率限制操作:创建包装在 Arc 中的 Semaphore,并设置最大许可数。对项目进行映射,生成任务以获取许可、处理并返回结果。使用 futures::future::join_all 收集结果。
库文档访问:
/rust-lang/rust - Rust 语言和标准库/tokio-rs/tokio - Tokio 异步运行时/tokio-rs/axum - Axum Web 框架/launchbadge/sqlx - SQLx 异步 SQL/serde-rs/serde - 序列化框架/dtolnay/thiserror - 错误派生/clap-rs/clap - CLI 解析器moai-lang-go - Go 系统编程模式moai-domain-backend - REST API 架构和微服务模式moai-foundation-quality - Rust 应用程序安全加固moai-workflow-testing - 测试驱动开发工作流常见问题:
性能特征:
请参阅 reference/engineering.md 了解高级所有权模式和特征设计。
请参阅 reference/performance.md 了解优化策略和分析技术。
请参阅 reference/guidelines.md 了解 Rust 编码标准和最佳实践。
最后更新:2026-01-11 版本:1.2.0
每周安装次数
58
代码仓库
GitHub 星标数
906
首次出现
2026 年 1 月 22 日
安全审计
安装于
opencode50
codex48
claude-code46
gemini-cli45
cursor43
antigravity41
Rust 1.92+ Development Specialist with deep patterns for high-performance, memory-safe applications.
Auto-Triggers: .rs, Cargo.toml, async/await, Tokio, Axum, SQLx, serde, lifetimes, traits
Core Use Cases:
Quick Patterns:
Axum REST API: Create Router with route macro chaining path and handler. Add with_state for shared state. Bind TcpListener with tokio::net and serve with axum::serve.
Async Handler with SQLx: Define async handler function taking State extractor for AppState and Path extractor for id. Use sqlx::query_as! macro with SQL string and bind parameters. Call fetch_optional on pool, await, and use ok_or for error conversion. Return Json wrapped result.
Modern Rust Features:
Async Traits (Stable): Define trait with async fn signatures. Implement trait for concrete types with async fn implementations. Call sqlx macros directly in trait methods.
Let-Else Pattern: Use let Some(value) = option else with return for early exit. Chain multiple let-else statements for sequential validation. Return error types in else blocks.
Installation: In Cargo.toml dependencies section, add axum version 0.8, tokio version 1.48 with full features, and tower-http version 0.6 with cors and trace features.
Complete API Setup: Import extractors from axum::extract and routing macros. Define Clone-derive AppState struct holding PgPool. In tokio::main async main, create pool with PgPoolOptions setting max_connections and connecting with DATABASE_URL from env. Build Router with route chains for paths and handlers, add CorsLayer, and call with_state. Bind TcpListener and call axum::serve.
Handler Patterns: Define async handlers taking State, Path, and Query extractors with appropriate types. Use sqlx::query_as! for type-safe queries with positional binds. Return Result with Json success and AppError failure.
Task Spawning and Channels: Create mpsc channel with capacity. Spawn worker tasks with tokio::spawn that receive from channel in loop. For timeouts, use tokio::select! macro with operation branch and sleep branch, returning error on timeout.
Type-Safe Queries: Derive sqlx::FromRow on structs for automatic mapping. Use query_as! macro for compile-time checked queries. Call fetch_one or fetch_optional on pool. For transactions, call pool.begin, execute queries on transaction reference, and call tx.commit.
Derive Serialize and Deserialize on structs. Use serde attribute with rename_all for case conversion. Use rename attribute for field-specific naming. Use skip_serializing_if with Option::is_none. Use default attribute for default values.
thiserror: Derive Error on enum with error attribute for display messages. Use from attribute for automatic conversion from source errors. Implement IntoResponse by matching on variants and returning status code with Json body containing error message.
Derive Parser on main Cli struct with command attributes for name, version, about. Use arg attribute for global flags. Derive Subcommand on enum for commands. Match on command in main to dispatch logic.
Create test module with cfg(test) attribute. Define tokio::test async functions. Call setup helpers, invoke functions under test, and use assert! macros for verification.
For comprehensive coverage including:
See: reference/engineering.md for ownership and traits, reference/performance.md for optimization, reference/guidelines.md for coding standards
Release Build: In Cargo.toml profile.release section, enable lto, set codegen-units to 1, set panic to abort, and enable strip.
Minimal Container: Use multi-stage Dockerfile. First stage uses rust alpine image, copies Cargo files, creates dummy main for dependency caching, builds release, copies source, touches main.rs for rebuild, builds final release. Second stage uses alpine, copies binary from builder, exposes port, and sets CMD.
Rate-Limited Operations: Create Arc-wrapped Semaphore with max permits. Map over items spawning tasks that acquire permit, process, and return result. Use futures::future::join_all to collect results.
Library Documentation Access:
/rust-lang/rust - Rust language and stdlib/tokio-rs/tokio - Tokio async runtime/tokio-rs/axum - Axum web framework/launchbadge/sqlx - SQLx async SQL/serde-rs/serde - Serialization framework/dtolnay/thiserror - Error derive/clap-rs/clap - CLI parsermoai-lang-go - Go systems programming patternsmoai-domain-backend - REST API architecture and microservices patternsmoai-foundation-quality - Security hardening for Rust applicationsmoai-workflow-testing - Test-driven development workflowsCommon Issues:
Performance Characteristics:
See reference/engineering.md for advanced ownership patterns and trait design.
See reference/performance.md for optimization strategies and profiling techniques.
See reference/guidelines.md for Rust coding standards and best practices.
Last Updated: 2026-01-11 Version: 1.2.0
Weekly Installs
58
Repository
GitHub Stars
906
First Seen
Jan 22, 2026
Security Audits
Gen Agent Trust HubFailSocketPassSnykWarn
Installed on
opencode50
codex48
claude-code46
gemini-cli45
cursor43
antigravity41
Lark Drive API 使用指南:飞书云文档、Wiki、表格 Token 处理与文件管理
48,300 周安装