跳转到内容
搜索 Fedora 文档

    浏览当前文档目录

    在 Fedora 上安装 Rust:1.98 与 crates 国内镜像

    Terminal window
    sudo dnf install cargo rust
    rustc --version
    cargo --version
    rustc 1.98.1 (48a229cea 2026-09-01) (Fedora 1.98.1-1.fc45)
    cargo 1.98.1 (797e8a9bc 2026-08-05) (Fedora 1.98.1-1.fc45)

    装 cargo 会把 rustc 一起带上,但显式装 rust 更清楚。二进制都在 /usr/bin。

    仓库 rustc/cargo 与 rustup 两条安装路线的取舍

    Terminal window
    cargo new hello
    cd hello
    cargo run

    cargo new 会生成 Cargo.toml 与 src/main.rs,cargo run 直接编译并执行。

    Terminal window
    cargo build --release
    ./target/release/hello

    cargo 拉依赖默认走 index.crates.io,国内经常慢。改配置:

    Terminal window
    mkdir -p ~/.cargo
    cat > ~/.cargo/config.toml <<'EOF'
    [source.crates-io]
    replace-with = 'tuna'
    [source.tuna]
    registry = "sparse+https://mirrors.tuna.tsinghua.edu.cn/crates.io-index/"
    EOF

    sparse+ 前缀很重要——它走 HTTP 索引而不是 git 克隆,速度快一个量级。

    验证:

    Terminal window
    cd ~/hello
    cargo add serde
    cargo build

    这是 Rust 最需要说清的一条:

    你要什么用哪个
    编译现成项目、CIdnf install cargo rust,跟 Fedora 升级
    固定某个版本rustup
    nightly / beta 工具链rustup(仓库版只有 stable)
    交叉编译、rustup target addrustup
    完全隔离容器 rust:1.98

    仓库版没有 rustup,也就没有 toolchain 切换、没有 rustup component add rustfmt/clippy(这两个在 Fedora 里是独立包)。

    需要 rustup:

    Terminal window
    curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
    source ~/.cargo/env
    rustup toolchain install nightly
    rustup default nightly

    国内加速 rustup 下载:

    Terminal window
    export RUSTUP_DIST_SERVER=https://mirrors.tuna.tsinghua.edu.cn/rustup
    export RUSTUP_UPDATE_ROOT=https://mirrors.tuna.tsinghua.edu.cn/rustup/rustup

    Fedora 版的常用附加组件装法:

    Terminal window
    sudo dnf install rustfmt clippy
    现象原因
    拉 crate 很慢或失败没配 crates 镜像,或用了 git 索引而非 sparse+
    cargo: command not found只装了 rust(只有 rustc),补装 cargo
    rustup 装的版本不生效/usr/bin/cargo 优先,which -a cargo 看顺序
    找不到 rustfmt仓库版来源是独立包 rustfmt,不是 rustup component
    编译内存不够链接阶段吃内存,用 -j1 或加 swap