CentOS 上高效的 Rust 调试技巧
一 基础与快速定位
let x = 5;
let y = dbg!(x * 2) + 1; // 输出: [src/main.rs:2] x * 2 = 10
[profile.dev]
debug = true
日常开发优先使用 cargo build 生成带有调试符号的调试构建。二 命令行调试器 GDB 与 LLDB
sudo yum install -y gdb lldb
cargo build
gdb target/debug/your_program
# 或使用 LLDB
lldb target/debug/your_program
cargo build --release -g
gdb target/release/your_program
rust-gdb target/debug/your_program
rust-lldb target/debug/your_program
break your_function if variable_name == 42
三 IDE 与可视化调试
四 内存与性能问题排查
valgrind --tool=memcheck target/debug/your_program
cargo test
cargo check
cargo install cargo-watch
cargo watch -x run
use log::{info, error};
env_logger::init();
info!("Starting the program");
if let Err(e) = run_program() {
error!("Error: {}", e);
}
运行时可通过环境变量控制日志级别:RUST_LOG=info cargo run
#[cfg(debug_assertions)]
fn debug_function() {
println!("Debug only");
}