且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

分步说明如何使用Msys2为Mingw安装Rust和Cargo?

更新时间:2023-09-29 18:12:40

您链接到日期的在Windows上使用Rust 页面的日期为rustup取代安装程序之前的默认选项,以安装Rust. 安装程序仍然可用,但是您应该使用如果可能的话,进行rustup,因为它使更新和一次使用多个工具链(例如稳定版,beta版和夜间版)变得容易.如果必须使用安装程序,只需选择x86_64-pc-windows-gnu安装程序,然后按照在Windows上使用Rust 页中的步骤进行操作.如果您正在使用rustup,请继续阅读.

The Using Rust on Windows page you linked to dates from before rustup replaced the installer as the default option to install Rust. Installers are still available, but you should use rustup if possible, because it makes it easy to update and to use multiple toolchains at once (e.g. stable, beta and nightly). If you must use the installer, just select the x86_64-pc-windows-gnu installer and follow the step from the Using Rust on Windows page. If you're using rustup, read on.

默认情况下,Windows上的rust安装针对MSVC工具链而不是GNU/MinGW-w64工具链的编译器和工具.在初始菜单中,选择 2)自定义安装.当要求输入主机三元组时,输入x86_64-pc-windows-gnu.然后选择其他问题,然后继续安装.

By default, rustup on Windows installs the compiler and tools targeting the MSVC toolchain, rather than the GNU/MinGW-w64 toolchain. At the initial menu, select 2) Customize installation. When asked for a host triple, enter x86_64-pc-windows-gnu. Then make a choice for the other questions, then proceed with the installation.

注意::如果已经安装了rustup,则重新运行rustup-init实际上不会安装请求的工具链.相反,如果您已经具有基于MSVC的工具链,请运行rustup toolchain install stable-x86_64-pc-windows-gnu.然后运行rustup default stable-x86_64-pc-windows-gnu将基于GNU的工具链设置为默认.

Note: If rustup is already installed, then rerunning rustup-init won't actually install the requested toolchain. Instead, run rustup toolchain install stable-x86_64-pc-windows-gnu if you already have the MSVC-based toolchain. Then run rustup default stable-x86_64-pc-windows-gnu to set the GNU-based toolchain as the default.

Rustup将自动安装MinGW链接器和平台库(作为rust-mingw组件的一部分),并拒绝您删除它们.如果您喜欢使用随MSYS2安装的MinGW链接器和库,则需要创建 .cargo/config 文件(位于您的配置文件目录(即C:\Users\you\.cargo\config中),或者位于此项目的目录中(如果此配置特定于项目).该文件的内容可能如下所示:

Rustup will install the MinGW linker and platform libraries automatically (as part of the rust-mingw component) and refuses to let you remove them. If you prefer to use the MinGW linker and libraries you installed with MSYS2, you'll need to create a .cargo/config file (either in your profile directory, i.e. C:\Users\you\.cargo\config, or in your project's directory if this configuration is specific to a project). The contents of that file might look like this:

[target.x86_64-pc-windows-gnu]
linker = "C:\\msys2\\mingw64\\bin\\gcc.exe"
ar = "C:\\msys2\\mingw64\\bin\\ar.exe"

Rustup会修改PATH环境变量,除非您告知您不要这样做.但是,MSYS2在启动时默认情况下会重置PATH,因此,当您尝试从MSYS2 shell调用cargorustc时,可能找不到它.您需要编辑.profile/.bash_profile脚本以正确设置PATH(您需要在/c/Users/yourname/.cargo/bin:之前添加到PATH).

Rustup will modify the PATH environment variable unless you told it not to. However, MSYS2 resets PATH by default when you launch, so when you try to invoke cargo or rustc from your MSYS2 shell, it might not find it. You'll need to edit your .profile/.bash_profile script to set the PATH correctly (you need to prepend /c/Users/yourname/.cargo/bin: to PATH).