温馨提示×

怎样在debian测rust

小樊
50
2026-10-16 20:40:53
栏目: 编程语言

在 Debian 上“测 Rust”通常指 安装 Rust 工具链并运行测试/示例程序。下面给你一套从零开始在 Debian 上验证 Rust 是否可用的常见做法。


一、在 Debian 上安装 Rust

方法 1(推荐):使用 rustup

sudo apt update
sudo apt install -y curl build-essential

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

安装完成后加载环境:

source "$HOME/.cargo/env"

验证版本:

rustc --version
cargo --version

方法 2:直接用 Debian 仓库(版本较旧)

sudo apt update
sudo apt install -y rustc cargo

二、写一个测试程序

mkdir hello && cd hello
cargo init --name hello

编辑 src/main.rs:

fn main() {
    println!("Hello from Rust on Debian!");
}

运行:

cargo run

三、写单元测试(Rust 自带的“测”)

修改 src/main.rs 或新建 src/lib.rs:

pub fn add(a: i32, b: i32) -> i32 {
    a + b
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_add() {
        assert_eq!(add(2, 3), 5);
    }
}

运行测试:

cargo test

四、常见“测 Rust”场景

1. 测系统是否支持 Rust 编译

cargo new test_env
cd test_env
cargo build

2. 测交叉编译(如 Debian → ARM)

rustup target add armv7-unknown-linux-gnueabihf
cargo build --target armv7-unknown-linux-gnueabihf

3. 测性能(benchmark)

cargo bench

五、排错提示

  • 权限问题:不要用 sudo 装 rustup
  • 找不到命令:确认 .cargo/bin 在 PATH
  • 旧 Debian:建议用 rustup 而非 apt

如果你说的“测 Rust”是指:

  • ✅ 测试 Rust 程序
  • ✅ 测试 Debian 是否支持 Rust
  • ✅ 性能/兼容性测试
  • ✅ CI 中测 Rust

可以告诉我具体目的,我给你更针对性的方案。

0 踩