首页主机资讯Rust在CentOS系统中的配置步骤

Rust在CentOS系统中的配置步骤

时间2025-12-15 21:02:03发布访客分类主机资讯浏览1320
导读:在 CentOS 上配置 Rust 的完整步骤 一 准备系统与依赖 更新系统并安装基础编译工具与包源: CentOS 8/Stream:sudo dnf install -y epel-release sudo dnf update -...

在 CentOS 上配置 Rust 的完整步骤

一 准备系统与依赖

  • 更新系统并安装基础编译工具与包源:
    • CentOS 8/Stream:
      sudo dnf install -y epel-release
      sudo dnf update -y
      sudo dnf groupinstall -y "Development Tools"
      sudo dnf install -y gcc make curl
      
    • CentOS 7:
      sudo yum install -y epel-release
      sudo yum update -y
      sudo yum groupinstall -y "Development Tools"
      sudo yum install -y gcc make curl
      
  • 说明:安装 gcc、make、curl 等工具可确保后续本地编译与网络下载顺利进行。

二 安装 Rust 工具链(rustup)

  • 使用官方安装脚本安装 rustup(Rust 版本管理器):
    curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
    
  • 按提示完成安装,随后在当前 shell 中加载环境变量:
    source "$HOME/.cargo/env"
    
  • 验证安装结果:
    rustc --version
    cargo --version
    
  • 如需更新到最新稳定版:
    rustup update
    
  • 如需常用开发工具:
    rustup component add rustfmt clippy
    
  • 说明:rustup 会将工具链安装到 ~/.cargo/bin,并自动写入环境变量配置。

三 配置国内镜像源(可选,提升下载速度)

  • 配置 rustup 镜像(安装阶段更快):
    # ~/.bashrc 或 ~/.zshrc
    export RUSTUP_DIST_SERVER=https://mirrors.tuna.tsinghua.edu.cn/rustup
    export RUSTUP_UPDATE_ROOT=https://mirrors.tuna.tsinghua.edu.cn/rustup/rustup
    
    使配置生效:source ~/.bashrc(或 source ~/.zshrc)。
  • 配置 Cargo 索引与注册表镜像(依赖下载更快):
    mkdir -p ~/.cargo
    cat >
         ~/.cargo/config.toml <
        <
    'EOF'
    [source.crates-io]
    registry = "https://github.com/rust-lang/crates.io-index"
    replace-with = "tuna"
    
    [source.tuna]
    registry = "https://mirrors.tuna.tsinghua.edu.cn/git/crates.io-index.git"
    
    [registries.rsproxy]
    index = "https://rsproxy.cn/crates.io-index"
    EOF
    
  • 说明:上述为常用镜像示例,亦可选择 USTC、SJTU、rsproxy 等镜像源。

四 快速验证与第一个程序

  • 验证工具链:
    rustc -V
    cargo -V
    
  • 使用 rustc 编译运行:
    echo 'fn main(){
        println!("Hello, Rust on CentOS");
    }
        ' >
         hello.rs
    rustc hello.rs
    ./hello
    
  • 使用 Cargo 创建并运行项目:
    cargo new hello_cargo
    cd hello_cargo
    cargo run
    
  • 说明:上述示例覆盖原生编译与 Cargo 工作流,适合作为环境验证。

五 常见问题与处理

  • 命令未找到:确认已执行 source "$HOME/.cargo/env",并检查 ~/.bashrc~/.zshrc 是否包含该加载语句;必要时重登或重启终端。
  • 权限问题:避免使用 root 日常开发;如需全局安装,请谨慎设置 CARGO_HOME/RUSTUP_HOME 并确保目录权限正确。
  • 代理环境:如必须走代理,可在 ~/.cargo/config.toml 中配置:
    [http]
    proxy = "http://127.0.0.1:7890"
    
    [https]
    proxy = "http://127.0.0.1:7890"
    
  • 镜像不可用:切换为其他可用镜像源(如 tuna、ustc、rsproxy),或临时移除镜像配置以直连官方源排查问题。

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: Rust在CentOS系统中的配置步骤
本文地址: https://pptw.com/jishu/772055.html
CentOS系统Rust环境配置指南 怎样提升centos的java性能

游客 回复需填写必要信息