官网:
rust语言官网:https://www.rust-lang.org/
在线编写:https://play.rust-lang.org/
windows系统下的rust集成开发环境:
在intellij idea里的plugins里安装rust插件,重启后即可创建rust工程。
rust编译工具
在 https://www.rust-lang.org/tools/install 下载rustup-init,并安装。
需要注意的是rust编译工具依赖c语言的编译工具,因此安装 rustup-init 时会提示:
Rust Visual C++ prerequisites
Rust requires the Microsoft C++ build tools for Visual Studio 2013 or
later, but they don't seem to be installed.
The easiest way to acquire the build tools is by installing Microsoft
Visual C++ Build Tools 2019 which provides just the Visual C++ build
tools:
https://visualstudio.microsoft.com/visual-cpp-build-tools/
Please ensure the Windows 10 SDK and the English language pack components
are included when installing the Visual C++ Build Tools.
Alternately, you can install Visual Studio 2019, Visual Studio 2017,
Visual Studio 2015, or Visual Studio 2013 and during install select
the "C++ tools":
https://visualstudio.microsoft.com/downloads/
Install the C++ build tools before proceeding.
If you will be targeting the GNU ABI or otherwise know what you are
doing then it is fine to continue installation without the build
tools, but otherwise, install the C++ build tools before proceeding.
Continue? (Y/n)
这段话的意思是rust需要Microsoft C++ build tools,有两种解决方案:
方式1:去https://visualstudio.microsoft.com/visual-cpp-build-tools/下载安装 Visual C++ build tools 即可。
方式2:也可以去https://visualstudio.microsoft.com/downloads/下载Visual Studio 2013及以后的版本,在安装的时候选择"C++ tools"。
这里使用的是方式1。后面有方式1的详细操作步骤(请见下面的 安装Microsoft C++ build tools的具体操作)。
选择上述两种方式的其中一种进行安装Microsoft C++ build tools,安装完成后再安装rustup-init。
rustup-init 安装完成后可以打开命令行,使用rustc -V
命令来确认是否安装成功:
rustc -V
rustc 1.51.0 (2fd73fabe 2021-03-23)
至此,rustup-init 安装完成。
rustup-init安装完成后可以查看rust的默认的工具链的路径以及标准库的路径:
1.rust的默认的工具链的路径是在用户目录的 .cargo\bin
目录下,cargo是rust的编译工具以及包管理工具。
可以看下rust官方https://www.rust-lang.org/learn/get-started中的关于Cargo的解释:
Cargo: the Rust build tool and package manager
When you install Rustup you’ll also get the latest stable version of the Rust build tool and package manager, also known as Cargo. Cargo does lots of things:
- build your project with
cargo build
- run your project with
cargo run
- test your project with
cargo test
- build documentation for your project with
cargo doc
- publish a library to crates.io with
cargo publish
2.rust标准库的路径是在用户目录的 .rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust
目录下。
安装Microsoft C++ build tools的具体操作
方式1:下载安装Visual C++ build tools
如果不想安装完整的Visual Studio,可以选择只安装build tools,然后在命令行下进行编译等操作,这样可以节省硬盘空间。
下载连接:https://visualstudio.microsoft.com/visual-cpp-build-tools/
下载下来的是这个文件:
这个是安装器Visual Studio Installer,双击打开后可以选择下载Visual C++ build tools。
双击上述下载的软件打开后的页面:
选择Visual Studio 生成工具 2019。
注意,一定要选择 使用C++的桌面开发
否则,后面使用cargo时会报错:
linker `link.exe` not found
此外,根据 rustup-init 安装时的提示:
Please ensure the Windows 10 SDK and the English language pack components
are included when installing the Visual C++ Build Tools.
必须要勾选上 Windows 10 SDK 以及 英语语言包 这两个组件:
勾选后,点击右下角的修改按钮,进行安装。
安装完成后重启系统。
方式2:下载安装Visual Studio 2013及以后的版本
https://visualstudio.microsoft.com/zh-hans/downloads/ 下载的是Visual Studio Installer:
可以看到其实和https://visualstudio.microsoft.com/visual-cpp-build-tools/链接下载下来的软件是很类似的。
因为Visual Studio 很庞大,功能极其多,因此先下载安装的是Visual Studio的安装器Visual Studio Installer,安装完成后打开Visual Studio Installer,然后选择性的安装Visual Studio的部分功能,这里要选择上C++ tools,然后安装。
安装完成后重启系统。
这里因为采用的是方式1,方式2需要另外实践测试…
创建一个简单的rust项目
新建一个rust项目:hello-rust
main.rs的内容如下:
fn main() {
println!("Hello, world!");
}
一个最简单的helloworld程序。
执行cargo run
C:\Users\test\rustProject\hello-rust\src>cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.00s
Running `C:\Users\test\rustProject\hello-rust\target\debug\hello-rust.exe`
Hello, world!
第一个简单程序运行成功!
执行cargo run
命令时,会在本目录,以及父目录去寻找Cargo.toml
文件,如果没有找到会报错,因此如果是在非rust项目下执行cargo run
,会报错:
C:\Users\test\otherProject>cargo run
error: could not find `Cargo.toml` in `C:\Users\test\otherProject` or any parent directory
学习资料
https://www.rust-lang.org/learn/get-started
https://www.runoob.com/rust/rust-tutorial.html
参考:
[Rust] Error: linker link.exe
not found
Windows下Rust build显示error: linker link.exe
not found问题解决办法
Unable to compile Rust hello world on Windows: linker link.exe not found
Rust在Windows环境编译error: linker link.exe
not found
Rust: link failed with exit code 1181