一、基础环境准备
1. 系统更新
bash
# Ubuntu/Debian系统
sudo apt update
sudo apt upgrade -y
# 安装基础开发工具
sudo apt install -y build-essential curl git pkg-config libssl-dev
2. Rust安装
bash
# 安装rustup
curl --proto '=https'--tlsv1.2-sSf https://sh.rustup.rs | sh
# 配置环境变量
source $HOME/.cargo/env
# 验证安装
rustc --version
cargo --version
二、开发工具配置
1. VS Code远程开发配置
本地安装VS Code
安装Remote-SSH插件
配置SSH连接:
json
// .ssh/config
Host rust-dev
HostName your-server-ip
User username
IdentityFile~/.ssh/id_rsa
2. Rust插件安装
bash
# 安装rust-analyzer
rustup component add rust-analyzer
# 安装代码格式化工具
rustup component add rustfmt
# 安装clippy代码检查工具
rustup component add clippy
三、项目管理工具
1. Cargo配置
toml
# ~/.cargo/config.toml
[source.crates-io]
registry ="https://github.com/rust-lang/crates.io-index"
replace-with='tuna'
[source.tuna]
registry ="https://mirrors.tuna.tsinghua.edu.cn/git/crates.io-index.git"
2. 项目初始化
bash
# 创建新项目
cargo new hello_rust
cd hello_rust
# 项目结构
.
├──Cargo.toml
└── src
└── main.rs
四、开发环境优化
1. 性能优化配置
bash
# 配置并行编译
echo "export RUSTC_PARALLEL=true">>~/.bashrc
# 配置链接器
echo "export RUSTFLAGS='-C target-cpu=native'">>~/.bashrc
2. 调试工具配置
bash
# 安装调试工具
sudo apt install -y gdb
# 配置VS Code调试
{
"version":"0.2.0",
"configurations":[
{
"type":"lldb",
"request":"launch",
"name":"Debug executable",
"cargo":{
"args":["build"],
"filter":{
"kind":"bin"
}
},
"args":[],
"cwd":"${workspaceFolder}"
}
]
}
五、依赖管理与构建
1. 依赖管理
toml
# Cargo.toml
[package]
name ="hello_rust"
version ="0.1.0"
edition ="2021"
[dependencies]
tokio ={ version ="1.0", features =["full"]}
serde ={ version ="1.0", features =["derive"]}
actix-web ="4.0"
2. 构建配置
bash
# 开发构建
cargo build
# 发布构建
cargo build --release
# 运行测试
cargo test
六、CI/CD配置
1. GitHub Actions配置
yaml
# .github/workflows/rust.yml
name:Rust CI
on:
push:
branches:[ main ]
pull_request:
branches:[ main ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name:Build
run: cargo build --verbose
- name:Run tests
run: cargo test --verbose
2. 自动部署脚本
bash
#!/bin/bash
# deploy.sh
cargo build --release
sudo systemctl stop rust-app
sudo cp target/release/hello_rust /usr/local/bin/
sudo systemctl start rust-app
七、监控与日志
1. 日志配置
rust
// 配置日志
use env_logger::{Builder,Env};
fn main(){
Builder::from_env(Env::default().default_filter_or("info"))
.init();
}
2. 性能监控
bash
# 安装性能监控工具
sudo apt install -y perf-tools-unstable
# 运行性能分析
perf record ./target/release/hello_rust
perf report