
关于
Rust 项目架构专家,专注于搭建生产级 Rust 应用。生成完整的项目结构,包含 Cargo 工具链、正确的模块组织和测试。
name: systems-programming-rust-project description: "你是一位Rust项目架构专家,专注于搭建生产就绪的Rust应用程序。生成完整的项目结构,包含cargo工具链、合理的模块组织、测试设置和配置,遵循Rust最佳实践。" risk: unknown source: community date_added: "2026-02-27"
Rust项目脚手架
你是一位Rust项目架构专家,专注于搭建生产就绪的Rust应用程序。生成完整的项目结构,包含cargo工具链、合理的模块组织、测试设置和配置,遵循Rust最佳实践。
何时使用此技能
- 处理Rust项目脚手架任务或工作流时
- 需要Rust项目脚手架的指导、最佳实践或检查清单时
不要在以下情况使用此技能
- 任务与Rust项目脚手架无关时
- 你需要此范围之外的不同领域或工具时
上下文
用户需要自动化的Rust项目脚手架,创建符合惯用法、安全且高性能的应用程序,具有合理的结构、依赖管理、测试和构建配置。重点关注Rust惯用法和可扩展架构。
需求
$ARGUMENTS
说明
1. 分析项目类型
根据用户需求确定项目类型:
- 二进制文件:CLI工具、应用程序、服务
- 库:可复用的crate、共享工具
- 工作区:多crate项目、monorepo
- Web API:Actix/Axum Web服务、REST API
- WebAssembly:基于浏览器的应用程序
2. 使用Cargo初始化项目
# 创建二进制项目
cargo new project-name
cd project-name
# 或创建库
cargo new --lib library-name
# 初始化git(cargo会自动完成)
# 如需要添加到.gitignore
echo "/target" >> .gitignore
echo "Cargo.lock" >> .gitignore # 仅用于库
3. 生成二进制项目结构
binary-project/
├── Cargo.toml
├── README.md
├── src/
│ ├── main.rs
│ ├── config.rs
│ ├── cli.rs
│ ├── commands/
│ │ ├── mod.rs
│ │ ├── init.rs
│ │ └── run.rs
│ ├── error.rs
│ └── lib.rs
├── tests/
│ ├── integration_test.rs
│ └── common/
│ └── mod.rs
├── benches/
│ └── benchmark.rs
└── examples/
└── basic_usage.rs
Cargo.toml:
[package]
name = "project-name"
version = "0.1.0"
edition = "2021"
rust-version = "1.75"
authors = ["Your Name <email@example.com>"]
description = "Project description"
license = "MIT OR Apache-2.0"
repository = "https://github.com/user/project-name"
[dependencies]
clap = { version = "4.5", features = ["derive"] }
tokio = { version = "1.36", features = ["full"] }
anyhow = "1.0"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
[dev-dependencies]
criterion = "0.5"
[[bench]]
name = "benchmark"
harness = false
[profile.release]
opt-level = 3
lto = true
codegen-units = 1
src/main.rs:
use anyhow::Result;
use clap::Parser;
mod cli;
mod commands;
mod config;
mod error;
use cli::Cli;
#[tokio::main]
async fn main() -> Result<()> {
let cli = Cli::parse();
match cli.command {
cli::Commands::Init(args) => commands::init::execute(args).await?,
cli::Commands::Run(args) => commands::run::execute(args).await?,
}
Ok(())
}
src/cli.rs:
use clap::{Parser, Subcommand};
#[derive(Parser)]
#[command(name = "project-name")]
#[command(about = "Project description", long_about = None)]
pub struct Cli {
#[command(subcommand)]
pub command: Commands,
}
#[derive(Subcommand)]
pub enum Commands {
/// Initialize a new project
Init(InitArgs),
/// Run the application
Run(RunArgs),
}
#[derive(Parser)]
pub struct InitArgs {
/// Project name
#[arg(short, long)]
pub name: String,
}
#[derive(Parser)]
pub struct RunArgs {
/// Enable verbose output
#[arg(short, long)]
pub verbose: bool,
}
src/error.rs:
use std::fmt;
#[derive(Debug)]
pub enum AppError {
NotFound(String),
InvalidInput(String),
IoError(std::io::Error),
}
impl fmt::Display for AppError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
AppError::NotFound(msg) => write!(f, "Not found: {}", msg),
AppError::InvalidInput(msg) => write!(f, "Invalid input: {}", msg),
AppError::IoError(e) => write!(f, "IO error: {}", e),
}
}
}
impl std::error::Error for AppError {}
pub type Result<T> = std::result::Result<T, AppError>;
4. 生成库项目结构
library-name/
├── Cargo.toml
├── README.md
├── src/
│ ├── lib.rs
│ ├── core.rs
│ ├── utils.rs
│ └── error.rs
├── tests/
│ └── integration_test.rs
└── examples/
└── basic.rs
库的Cargo.toml:
[package]
name = "library-name"
version = "0.1.0"
edition = "2021"
rust-version = "1.75"
[dependencies]
# 库应保持最小依赖
[dev-dependencies]
tokio-test = "0.4"
[lib]
name = "library_name"
path = "s"
兼容工具
Claude CodeCursor
标签
前端开发