下面给你一份从 零开始在 Debian 上运行 Rust 项目 的实用流程,覆盖常见情况(新项目 / 已有项目 / 服务器运行)。
sudo apt update
sudo apt upgrade -y
sudo apt install -y build-essential curl git
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
按提示选择 1) Default installation
source $HOME/.cargo/env
rustc --version
cargo --version
cargo new my_project
cd my_project
目录结构:
my_project
├── Cargo.toml
└── src
└── main.rs
cargo run
输出类似:
Hello, world!
git clone https://github.com/xxx/xxx.git
cd xxx
cargo run
如果是 release 模式(性能更好):
cargo run --release
cargo build
# 或
cargo build --release
生成文件:
target/debug/项目名
target/release/项目名
直接运行:
./target/release/项目名
cargo build --release
nohup ./target/release/项目名 &
创建服务文件:
sudo nano /etc/systemd/system/rustapp.service
示例:
[Unit]
Description=Rust App
After=network.target
[Service]
User=youruser
WorkingDirectory=/home/youruser/my_project
ExecStart=/home/youruser/my_project/target/release/项目名
Restart=always
[Install]
WantedBy=multi-user.target
启用:
sudo systemctl daemon-reload
sudo systemctl enable rustapp
sudo systemctl start rustapp
查看状态:
sudo systemctl status rustapp
cargo: command not foundsource $HOME/.cargo/env
或写入 .bashrc:
echo 'source $HOME/.cargo/env' >> ~/.bashrc
--releaserustup install 1.75.0
rustup default 1.75.0
如果你愿意,可以告诉我:
我可以直接给你更精确的运行方案。