在Windows上搭建Rust开发环境

文章目录

  • 安装前的准备
  • 安装mingw64
  • 安装Rust
  • hello world
  • 安装并配置vscode
    • 安装
    • 汉化
    • 安装TODO tree
    • 安装Rust插件
    • 安装调试插件
    • 安装Setings Sync
  • 使用vscode调试

安装前的准备

在Windows上搭建Rust开发环境并不困难,前面提到Rust只有编译器没有链接器,还需要C/C++的链接器。所以我们需要先安装C/C++的编译链,Windows上C/C++的编译链有微软提供的visual studio和GNU的mingw,visual studio太过庞大了,我选择使用mingw64作为C/C++的编译链,使用vscode作为Rust的开发工具。

安装mingw64

  1. mingw的下载托管在sourceforge,点击进入后选择x86_64-posix-seh进行下载(下载有些慢,还请耐心等待)。
  2. 此版本是一个免安装版本,可以直接解压在你想要的目录下(最好不要有中文)。
  3. 修改环境变量PATH,添加解压目录下的bin 在Windows上搭建Rust开发环境_第1张图片
  4. 打开cmd窗口,输入gcc --version,如果显示如下内容,则配置成功:
    gcc.exe (x86_64-posix-seh-rev0, Built by MinGW-W64 project) 8.1.0
    Copyright (C) 2018 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions.  There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    

注:
mingw版本号中有三个字段分别代表如下含义:

  • x86_64:64位版本
  • i686:32位版本
  • posix:操作系统接口标准为posix,相比win32,posix对C++11的标准库支持的更好
  • win32:操作系统接口标准为win32
  • sjlj:采用sjlj的异常处理,这种方式比起其他异常处理会慢得多
  • dwarf:采用dwarf的异常处理,这种方式需要在可执行程序中添加额外的调试信息,使得程序体积较大
  • seh:采用seh的异常处理,即使用windows自身的异常处理机制

安装Rust

  1. 从Rust的官网下载rustup-init
  2. rustup-init为在线安装工具,默认的安装源为国外的站点,安装较慢,可设置环境变量,使其从科大的镜像中下载:
    • RUSTUP_DIST_SERVER=https://mirrors.ustc.edu.cn/rust-static
    • RUSTUP_UPDATE_ROOT=https://mirrors.ustc.edu.cn/rust-static/rustup
      在Windows上搭建Rust开发环境_第2张图片
  3. 打开cmd窗口,运行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/downloads/#build-tools-for-visual-studio-2019
    
    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)
    
  4. 意思是首先要安装C/C++的编译环境,我们已经安装了mingw,直接Y,又会输出下面的内容:
    Welcome to Rust!                                                             
    This will download and install the official compiler for the Rust programming   
    language, and its package manager, Cargo. 										
    It will add the cargo, rustc, rustup and other commands to Cargo's bin          
    directory, located at:	
    	C:\Users\zhang\.cargo\bin  													
    This can be modified with the CARGO_HOME environment variable. 														
    Rustup metadata and toolchains will be installed into the Rustup home           
    directory, located at: 
    	C:\Users\zhang\.rustup  														
    This can be modified with the RUSTUP_HOME environment variable. 															
    This path will then be added to your PATH environment variable by modifying the 
    HKEY_CURRENT_USER/Environment/PATH registry key. 																		
    You can uninstall at any time with rustup self uninstall and these changes will 
    be reverted.																	
    Current installation options:													
    	default host triple: x86_64-pc-windows-msvc                                  
    	default toolchain: stable                                                  
    	modify PATH variable: yes													
    1) Proceed with installation (default)
    2) Customize installation   
    3) Cancel installation
    
  5. 开始介绍了一下将cargo、rustc、rustup和其他的工具安装到用户主目录的.cargo\bin目录中,将rust的工具链安装到用户主目录的.rustup目录中。然后询问要安装rust的哪个工具链,默认的是微软的windows-msvc,因此,我们要修改一下默认的选项,选择2
    I'm going to ask you the value of each of these installation options.
    You may simply press the Enter key to leave unchanged.
    
    Default host triple?
    
  6. 输入x86_64-pc-windows-gnu,表示我要安装64位的gnu版本
    Default toolchain? (stable/beta/nightly/none)
    
  7. 选择默认的工具链为stable,即稳定版本:
    Modify PATH variable? (y/n)
    
  8. 选择y允许安装程序修改PATH环境变量:
    Current installation options:
    
    	default host triple: x86_64-pc-windows-gnu
    	default toolchain: stable
    	modify PATH variable: yes
    
    1) Proceed with installation (default)
    2) Customize installation
    3) Cancel installation
    
  9. 默认的安装选项已经被修改,此时输入1开始安装。安装过程中要从网络下载很多东西,请耐心等待。
  10. 安装完成后,在命令行窗口中输入rustc --version,输出:
    rustc 1.40.0 (73528e339 2019-12-16)
    
    表示安装成功。
  11. cargo是rust的包管理工具,类似于python的pip,默认的cargo会从国外网站下载包,也可以修改为从科大下载。在用户主目录的.cargo目录下新建一个文件,命名为config(没有扩展名),并输入以下内容:
    [source.crates-io]
    registry = "https://github.com/rust-lang/crates.io-index"
    replace-with = 'ustc'
    [source.ustc]
    registry = "git://mirrors.ustc.edu.cn/crates.io-index"
    

经过这么多步骤,rust终于安装完成了。

hello world

经过了一系列的安装配置,终于可以动手写hello world了。
首先,我们通过cargo来创建项目,在cmd窗口中输入

cargo new hello --bin
     Created binary (application) `hello` package

cargo已经帮我们创建好了hello项目,自动生成了一系列文件:

D:\TEST\RUST\HELLO
|   .gitignore
|   Cargo.toml
|
\---src
        main.rs

其中src下的main.rs为rust代码文件,其内容为:

fn main() {
    println!("Hello, world!");
}

使用如下命令可以直接运行这段代码:

cd hello
cargo run
   Compiling hello v0.1.0 (D:\test\rust\hello)
    Finished dev [unoptimized + debuginfo] target(s) in 1.36s
     Running `target\debug\hello.exe`
Hello, world!

安装并配置vscode

安装

接下来就可以安装vscode了。从官网下载安装包,并一步一步安装。安装过程中,最好勾选下面的选项:
在Windows上搭建Rust开发环境_第3张图片

汉化

  1. 打开插件管理器
  2. 在最上方搜索chinese
    在Windows上搭建Rust开发环境_第4张图片
  3. 点击上图列表中第一条后面绿色的Install
  4. 安装完成后,vscode的右下角弹出需要重启的提示,点击重启即可。

安装TODO tree

  1. 打开插件管理器
  2. 在最上方搜索todo tree
  3. 点击插件列表中绿色的Install
  4. 安装完成后,在插件管理器下方出现了TODO tree的图标
  5. 如果你在代码里写了带有TODO的注释,TODO Tree就会将一切待办事项清晰的展现在你眼前

安装Rust插件

  1. 打开插件管理器
  2. 在最上方搜索rust,选择Rust(rls)并点击安装
  3. 安装完成后,如下图所示:
    在Windows上搭建Rust开发环境_第5张图片

安装调试插件

  1. 打开插件管理器
  2. 在最上方搜索codelldb,并点击安装
    在Windows上搭建Rust开发环境_第6张图片
  3. 安装过程中vscode会从github上下载vscode-lldb-x86_64-windows,下载完成后自动安装

安装Setings Sync

Setings Sync可以将你对vscode的配置上传到GitHub,这样就可以在多台机器上同步vscode配置。

  1. 打开插件管理器
  2. 在最上方搜索Setings Sync
  3. 点击Setings Sync后面绿色的Install
  4. 安装完成后,自动打开配置页面
    在Windows上搭建Rust开发环境_第7张图片
  5. 点击【LONGIN WITH GITHUB】,在弹出的浏览器中输入你的GitHub账号
  6. 登录成功后,自动配置选择页面,选择页面中显示你曾经使用并上传过配置的列表
    在Windows上搭建Rust开发环境_第8张图片
  7. 从列表中选择一个Gist,即可将该vscode的配置与这个Gist同步
  8. 你也可以点击下面的【SKIP】,此时你的这个vscode将不与任何其他的vscode同步,仅上传下载自己的配置信息
  9. 选择后,Setings Sync保存你的Gist ID
    在Windows上搭建Rust开发环境_第9张图片
  10. 每次更改配置后,Setings Sync都会自动上传配置;每次启动vscode时,Setings Sync都会自动下载配置
  11. 你可以按Shift+Alt+U手动上传配置,按Shift+Alt+D手动下载配置

使用vscode调试

用vscode打开hello目录,注意是打开hello目录,而不是main.rs文件。
然后在vscode中打开main.rs文件,第一次启动时,vscode会提示安装rls,如:
在这里插入图片描述
点击【Yes】,vscode自动会帮我们安装好,安装完成后提示:
在这里插入图片描述

在代码行号前点击左键,可以设置断点:
在Windows上搭建Rust开发环境_第10张图片
切换到调试页面,点击创建lauch.json文件:
在Windows上搭建Rust开发环境_第11张图片
在弹出的选择框中选择LLDB:
在Windows上搭建Rust开发环境_第12张图片
在弹出的窗口中选择Yes:
在Windows上搭建Rust开发环境_第13张图片
按F5或点击调试按钮,即可进行调试:
在Windows上搭建Rust开发环境_第14张图片

你可能感兴趣的:(Rust语言学习笔记)