发布于2026-06-29 阅读(0)
扫一扫,手机访问
在Linux生态中,Debian以其稳定性和广泛的软件支持,成为不少开发者首选的服务器或开发环境。如果你正打算在Debian上运行Rust项目,那么接下来的几个步骤,基本能覆盖从环境搭建到服务部署的全过程。整个过程并不复杂,但有几个关键细节值得留意。
运行Rust项目,第一步自然是搞定编译器(rustc)和包管理工具(cargo)。最推荐的方案是通过官方工具rustup来安装,这样能确保版本是最新的,后续升级也方便。

sudo apt updatecurl,顺手装一下:sudo apt install curlcurl下载并运行rustup安装脚本:curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | shsource $HOME/.cargo/envrustc --version,如果看到版本号,说明环境已经就绪。你可以新建一个项目,也可以直接用现有的。新建的话,在终端敲cargo new 项目名称(比如cargo new hello_rust),Cargo会自动生成标准的项目骨架:src/main.rs是主程序文件,Cargo.toml是配置文件。然后进入项目目录:cd 项目名称。
用你习惯的文本编辑器(比如nano,或者从远程连过来的VS Code)打开src/main.rs,开始写Rust代码。最简单的例子——“Hello World”程序:
fn main() {
println!("Hello, Debian!");
}
保存文件后回到终端,就可以准备编译了。
cargo run,Cargo会自动完成编译并执行。输出类似:Compiling hello_rust v0.1.0 (/path/to/hello_rust)
Finished dev [unoptimized + debuginfo] target(s) in 0.50s
Running `target/debug/hello_rust`
Hello, Debian!
开发模式编译速度快,包含调试信息,适合日常调试。cargo build --release编译。生成的可执行文件在target/release/项目名称(比如target/release/hello_rust)。运行它:./target/release/hello_rust
发布版本去掉了调试信息,性能更优。Rust的依赖管理非常简洁,所有第三方库都在Cargo.toml的[dependencies]部分声明。例如,想引入serde(一个常用的JSON序列化库),只需要在Cargo.toml里添加:
[dependencies]
serde = "1.0"
然后运行cargo build,Cargo会自动下载serde及其所有依赖,并缓存到本地(~/.cargo/registry)。之后每次构建都会直接使用本地缓存,速度很快。
如果你写的是一个后台服务(比如Web API),可以用systemd把它注册为系统服务,实现开机自启和崩溃重启。操作方式如下:
sudo nano /etc/systemd/system/your_project.service[Unit]
Description=Your Rust Project
After=network.target
[Service]
User=your_username
Group=your_group
ExecStart=/path/to/your_project/target/release/your_executable
Restart=always
[Install]
WantedBy=multi-user.targetsudo systemctl enable your_project
sudo systemctl start your_projectsudo systemctl status your_project~/.cargo/config.toml中添加:[source.crates-io]
replace-with = 'ustc'
[source.ustc]
registry = "https://mirrors.ustc.edu.cn/crates.io-index"chmod +x target/release/your_executable按照上述流程,你就能在Debian上顺利运行、管理并部署Rust项目了。整个过程几乎没有额外的坑,只要顺着Cargo的习惯走,一般都很顺畅。
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
正版软件
正版软件
正版软件
正版软件
正版软件
1
2
3
7
8