debian

如何在Debian上配置Rust开发环境

小樊
44
2025-08-20 06:27:28
栏目: 编程语言

在Debian上配置Rust开发环境可按以下步骤操作:

  1. 更新系统并安装依赖

    sudo apt update  
    sudo apt install curl build-essential gcc make -y  
    
  2. 安装Rust工具链(推荐使用rustup)

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

    按提示完成安装,可选择将Rust安装到系统目录(如/opt/rust)或用户目录。

  3. 配置环境变量

    • 若安装时未自动配置,手动添加以下内容到~/.bashrc~/.zshrc
      source $HOME/.cargo/env  
      
    • 或系统级配置(需sudo):
      echo 'export PATH="$HOME/.cargo/bin:$PATH"' | sudo tee -a /etc/profile.d/rust.sh  
      source /etc/profile  
      
  4. 验证安装

    rustc --version  # 查看Rust编译器版本  
    cargo --version  # 查看Cargo版本  
    
  5. (可选)安装额外工具

    • 代码格式化:rustup component add rustfmt
    • 代码检查:rustup component add clippy
  6. (可选)配置镜像加速
    编辑~/.cargo/config.toml,添加国内镜像源(如清华源):

    [source.crates-io]  
    replace-with = "tuna"  
    [source.tuna]  
    registry = "https://mirrors.tuna.tsinghua.edu.cn/git/crates.io-index.git"  
    
  7. 创建并运行Rust项目

    cargo new hello_world  
    cd hello_world  
    cargo build  
    cargo run  
    

安装完成后,可使用VSCode等IDE配合Rust插件(如rust-analyzer)提升开发体验。

0
看了该问题的人还看了