Claude Code Rust:Claude Code 实战指南
了解claude code rust:Claude Code 实战. 包含实用技巧和代码示例。
Rust开发でClaude Code 活躍する場面
Rustは所有権、ライフタイム、トレイトなど独自の概念が多く、学習コストの高い言語です。Claude Code 这らの概念を正確に理解し、编译が通るコードを高い精度で生成可以。尤其错误处理や异步処理の定类型パターンで威力を発揮します。
CLIツールの开发
> clap使用...的CLIツール创建。
> JSON文件を読み込んでCSVに変換する機能で。
> プ日志レスバーと错误处理も实现して。
// src/main.rs
use clap::Parser;
use anyhow::{Context, Result};
use indicatif::{ProgressBar, ProgressStyle};
use serde_json::Value;
use std::fs;
use std::io::Write;
#[derive(Parser, Debug)]
#[command(name = "json2csv", about = "Convert JSON to CSV")]
struct Args {
/// Input JSON file path
#[arg(short, long)]
input: String,
/// Output CSV file path
#[arg(short, long)]
output: String,
/// Fields to extract (comma-separated)
#[arg(short, long)]
fields: Option<String>,
}
fn main() -> Result<()> {
let args = Args::parse();
let content = fs::read_to_string(&args.input)
.context(format!("Failed to read file: {}", args.input))?;
let data: Vec<Value> = serde_json::from_str(&content)
.context("Failed to parse JSON")?;
if data.is_empty() {
println!("No data found in the JSON file.");
return Ok(());
}
// フィールドの決定
let fields: Vec<String> = match &args.fields {
Some(f) => f.split(',').map(|s| s.trim().to_string()).collect(),
None => {
if let Some(obj) = data[0].as_object() {
obj.keys().cloned().collect()
} else {
anyhow::bail!("JSON array elements must be objects");
}
}
};
let pb = ProgressBar::new(data.len() as u64);
pb.set_style(
ProgressStyle::with_template(
"{spinner:.green} [{elapsed_precise}] [{bar:40.cyan/blue}] {pos}/{len} ({eta})"
)?
.progress_chars("=>-"),
);
let mut output = fs::File::create(&args.output)
.context(format!("Failed to create output file: {}", args.output))?;
// Header
writeln!(output, "{}", fields.join(","))?;
// 数据行
for item in &data {
let row: Vec<String> = fields.iter().map(|field| {
match item.get(field) {
Some(Value::String(s)) => format!("\"{}\"", s.replace('"', "\"\"")),
Some(v) => v.to_string(),
None => String::new(),
}
}).collect();
writeln!(output, "{}", row.join(","))?;
pb.inc(1);
}
pb.finish_with_message("Done");
println!("Converted {} records to {}", data.len(), args.output);
Ok(())
}
# Cargo.toml
[package]
name = "json2csv"
version = "0.1.0"
edition = "2021"
[dependencies]
clap = { version = "4", features = ["derive"] }
serde_json = "1"
anyhow = "1"
indicatif = "0.17"
Web APIの开发(Axum)
> Axum使用...的REST API创建。
> CRUD操作、验证、错误处理实现。
// src/main.rs
use axum::{
extract::{Path, State},
http::StatusCode,
response::IntoResponse,
routing::{get, post, put, delete},
Json, Router,
};
use serde::{Deserialize, Serialize};
use sqlx::PgPool;
use uuid::Uuid;
#[derive(Debug, Serialize, Deserialize, sqlx::FromRow)]
struct Task {
id: Uuid,
title: String,
description: Option<String>,
completed: bool,
created_at: chrono::NaiveDateTime,
}
#[derive(Debug, Deserialize)]
struct CreateTask {
title: String,
description: Option<String>,
}
#[derive(Debug, Deserialize)]
struct UpdateTask {
title: Option<String>,
description: Option<String>,
completed: Option<bool>,
}
#[derive(Clone)]
struct AppState {
db: PgPool,
}
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let db = PgPool::connect(&std::env::var("DATABASE_URL")?).await?;
sqlx::migrate!().run(&db).await?;
let state = AppState { db };
let app = Router::new()
.route("/api/tasks", get(list_tasks).post(create_task))
.route("/api/tasks/:id", get(get_task).put(update_task).delete(delete_task))
.with_state(state);
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await?;
println!("Server running on http://localhost:3000");
axum::serve(listener, app).await?;
Ok(())
}
async fn list_tasks(State(state): State<AppState>) -> impl IntoResponse {
let tasks = sqlx::query_as::<_, Task>("SELECT * FROM tasks ORDER BY created_at DESC")
.fetch_all(&state.db)
.await;
match tasks {
Ok(tasks) => Json(tasks).into_response(),
Err(e) => (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()).into_response(),
}
}
async fn create_task(
State(state): State<AppState>,
Json(input): Json<CreateTask>,
) -> impl IntoResponse {
if input.title.trim().is_empty() {
return (StatusCode::BAD_REQUEST, "Title is required").into_response();
}
let task = sqlx::query_as::<_, Task>(
"INSERT INTO tasks (id, title, description) VALUES ($1, $2, $3) RETURNING *"
)
.bind(Uuid::new_v4())
.bind(&input.title)
.bind(&input.description)
.fetch_one(&state.db)
.await;
match task {
Ok(task) => (StatusCode::CREATED, Json(task)).into_response(),
Err(e) => (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()).into_response(),
}
}
async fn get_task(
State(state): State<AppState>,
Path(id): Path<Uuid>,
) -> impl IntoResponse {
let task = sqlx::query_as::<_, Task>("SELECT * FROM tasks WHERE id = $1")
.bind(id)
.fetch_optional(&state.db)
.await;
match task {
Ok(Some(task)) => Json(task).into_response(),
Ok(None) => StatusCode::NOT_FOUND.into_response(),
Err(e) => (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()).into_response(),
}
}
async fn update_task(
State(state): State<AppState>,
Path(id): Path<Uuid>,
Json(input): Json<UpdateTask>,
) -> impl IntoResponse {
let task = sqlx::query_as::<_, Task>(
"UPDATE tasks SET title = COALESCE($2, title), description = COALESCE($3, description), completed = COALESCE($4, completed) WHERE id = $1 RETURNING *"
)
.bind(id)
.bind(&input.title)
.bind(&input.description)
.bind(input.completed)
.fetch_optional(&state.db)
.await;
match task {
Ok(Some(task)) => Json(task).into_response(),
Ok(None) => StatusCode::NOT_FOUND.into_response(),
Err(e) => (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()).into_response(),
}
}
async fn delete_task(
State(state): State<AppState>,
Path(id): Path<Uuid>,
) -> impl IntoResponse {
let result = sqlx::query("DELETE FROM tasks WHERE id = $1")
.bind(id)
.execute(&state.db)
.await;
match result {
Ok(r) if r.rows_affected() > 0 => StatusCode::NO_CONTENT.into_response(),
Ok(_) => StatusCode::NOT_FOUND.into_response(),
Err(e) => (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()).into_response(),
}
}
错误处理のパターン
让 Claude Code依頼する際、Rustらしい错误处理を指示可以。
> カスタム错误类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类类型をthiserrorで定義して。
> API响应への変換も实现して。
use axum::{http::StatusCode, response::IntoResponse, Json};
use thiserror::Error;
#[derive(Error, Debug)]
pub enum AppError {
#[error("Not found: {0}")]
NotFound(String),
#[error("Validation error: {0}")]
Validation(String),
#[error("Database error: {0}")]
Database(#[from] sqlx::Error),
#[error("Internal error: {0}")]
Internal(#[from] anyhow::Error),
}
impl IntoResponse for AppError {
fn into_response(self) -> axum::response::Response {
let (status, message) = match &self {
AppError::NotFound(msg) => (StatusCode::NOT_FOUND, msg.clone()),
AppError::Validation(msg) => (StatusCode::BAD_REQUEST, msg.clone()),
AppError::Database(_) => (StatusCode::INTERNAL_SERVER_ERROR, "Database error".into()),
AppError::Internal(_) => (StatusCode::INTERNAL_SERVER_ERROR, "Internal error".into()),
};
(status, Json(serde_json::json!({ "error": message }))).into_response()
}
}
总结
借助 Claude Code,Rustの所有権やライフタイムを正确地扱ったコードを高效地生成可以。CLIツール、Web API、错误处理など、Rust特有のパターンに精通しているため、编译错误との戦いを大幅に減らせます。他の言語との比較はClaude Code vs GitHub Copilot。効率的な使い方は生産性を3倍にするTipsで紹介しています。
Claude Code 的详细信息请参阅Anthropic官方文档。Rustの学習にはThe Rust Programming Languageが最適です。
免费 PDF:5 分钟看懂 Claude Code 速查表
只需留下邮箱,我们就会立即把这份 A4 一页速查表 PDF 发送给你。
我们会严格保护你的个人信息,绝不发送垃圾邮件。
把 Claude Code 变成真正能带来结果的工作流
先领取中文说明的免费 PDF,再进入英文商品页选择合适的教材。如果你需要团队落地、流程设计或内容变现支持,也可以直接咨询。
本文作者
Masa
深度使用 Claude Code 的工程师。运营 claudecode-lab.com——一个涵盖 10 种语言、超过 2,000 页内容的科技媒体。
相关文章
每天发布多语言 Claude Code 文章前,要先检查的 7 件事
一份实用清单,帮助你每天发布多语言 Claude Code 文章时避免漏语言、CTA 错位和线上内容未更新。
Codex Automations 是什么?让 AI 在你睡觉时完成内容运营
用 Codex Automations 自动查看流量、选择主题、写文章、改善转化路径并部署网站的实用指南。
Claude Code × GCP Cloud Functions 完全指南 | 极速开发无服务器函数
用 Claude Code 高效开发 GCP Cloud Functions。从 HTTP/Pub/Sub/Firestore 触发器实现到本地测试、部署自动化,基于 Masa 的实战经验,附完整可运行代码示例。