Windows 11 上通过国内源安装 Rust

文章目录

    下载

    首先,从 Rust 官方网站下载 rustup-init.exe 64 位安装包。

    https://www.rust-lang.org/tools/install

    网络连接错误

    但是直接点击 rustup-init.exe 进行安装,会报错。大意是网络无法连接。

    这个跟之前在 Linux 上安装 rust 是一样的问题,被网址被国内和谐了。

    info: profile set to 'default'
    info: default host triple is x86_64-pc-windows-msvc
    info: syncing channel updates for 'stable-x86_64-pc-windows-msvc'
    error: could not download file from 'https://static.rust-lang.org/dist/channel-rust-stable.toml.sha256' to 'C:\Users\Lenovo\.rustup\tmp\_rjoemu64lntqth4_file': failed to make network request: error sending request for url (https://static.rust-lang.org/dist/channel-rust-stable.toml.sha256): error trying to connect: tcp connect error: 由于连接方在一段时间后没 有正确答复或连接的主机没有反应,连接尝试失败。 (os error 10060): error trying to connect: tcp connect error: 由于连接方 在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。 (os error 10060): tcp connect error: 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。 (os error 10060): 由于连接方在一段时间后没有正确答复或连接的主机没有反 应,连接尝试失败。 (os error 10060)
    
    Press the Enter key to continue.
    

    配置国内源

    进入到保存 rustup-init.exe 的文件目录。

    同时按住 Shift 键及鼠标右键,在弹出菜单中,选择 “在此处打开 Powershell 窗口”。(又学到了一招 Windows 11 小技巧。。。)

    在此处打开 Powershell 窗口

    注意,不按 Shift 键,是没有这个 Powershell菜单选项的。

    设置国内源的环境变量:

    $ENV:RUSTUP_DIST_SERVER='https://mirrors.ustc.edu.cn/rust-static' 
    $ENV:RUSTUP_UPDATE_ROOT='https://mirrors.ustc.edu.cn/rust-static/rustup'
    

    然后再重新执行 rust 安装程序:

    .\rustup-init.exe
    

    安装过程日志

    再次安装就非常快了

    info: profile set to 'default'
    info: default host triple is x86_64-pc-windows-msvc
    info: syncing channel updates for 'stable-x86_64-pc-windows-msvc'
    info: latest update on 2024-03-28, rust version 1.77.1 (7cf61ebde 2024-03-27)
    info: downloading component 'cargo'
    info: downloading component 'clippy'
    info: downloading component 'rust-docs'
     14.9 MiB /  14.9 MiB (100 %)   5.3 MiB/s in  2s ETA:  0s
    info: downloading component 'rust-std'
     18.2 MiB /  18.2 MiB (100 %)   7.1 MiB/s in  3s ETA:  0s
    info: downloading component 'rustc'
     58.1 MiB /  58.1 MiB (100 %)   6.4 MiB/s in 23s ETA:  0s
    info: downloading component 'rustfmt'
    info: installing component 'cargo'
    info: installing component 'clippy'
    info: installing component 'rust-docs'
     14.9 MiB /  14.9 MiB (100 %)   1.8 MiB/s in 26s ETA:  0s
    info: installing component 'rust-std'
     18.2 MiB /  18.2 MiB (100 %)  16.3 MiB/s in  1s ETA:  0s
     11 IO-ops /  11 IO-ops (100 %)   0 IOPS in  9s ETA: Unknown
    info: installing component 'rustc'
     58.1 MiB /  58.1 MiB (100 %)  17.5 MiB/s in  3s ETA:  0s
     12 IO-ops /  12 IO-ops (100 %)   0 IOPS in 16s ETA: Unknown
    info: installing component 'rustfmt'
    info: default toolchain set to 'stable-x86_64-pc-windows-msvc'
    
      stable-x86_64-pc-windows-msvc installed - rustc 1.77.1 (7cf61ebde 2024-03-27)
    
    
    Rust is installed now. Great!
    
    To get started you may need to restart your current shell.
    This would reload its PATH environment variable to include
    Cargo's bin directory (%USERPROFILE%\.cargo\bin).
    
    Press the Enter key to continue.
    

    测试

    关闭当前 Powershell 窗口,再次打开,就能使用 rust 相关工具了。

    > rustc.exe --version
    rustc 1.77.1 (7cf61ebde 2024-03-27)
    

    cargo 国内源

    如果需要后续使用 cargo 安装依赖包,继续使用国内源,还需修改 windows 系统环境变量。

    在 windows 当前用户目录下,找到 .cargo 目录,在其下面新建一个 config 文件

    cargo config 目录

    内容:

    [source.crates-io]
    registry = "https://github.com/rust-lang/crates.io-index"
    
    # 替换成你偏好的镜像源
    replace-with = 'ustc'
    
    # 清华大学
    [source.tuna]
    registry = "https://mirrors.tuna.tsinghua.edu.cn/git/crates.io-index.git"
    
    # 中国科学技术大学
    [source.ustc]
    registry = "git://mirrors.ustc.edu.cn/crates.io-index"
    
    # 上海交通大学
    [source.sjtu]
    registry = "https://mirrors.sjtug.sjtu.edu.cn/git/crates.io-index"
    
    # rustcc社区
    [source.rustcc]
    registry = "git://crates.rustcc.cn/crates.io-index"
    

    保存即可

    参考

    https://www.cnblogs.com/manqing321/p/17026725.html

    关于作者 🌱

    我是来自山东烟台的一名开发者,有感兴趣的话题,或者软件开发需求,欢迎加微信 zhongwei 聊聊,或者关注我的个人公众号“大象工具”, 查看更多联系方式