在Windows上搭建Rust开发环境——Clion篇

在Windows上搭建Rust开发环境——Clion篇

刚开始学习Rust的时候,写过一篇《在Windows上搭建Rust开发环境》,当时选择使用VSCode作为开发工具。由于工作的原因,放下Rust很久了,Rust的生态也发生了很大的变化。听闻Jetbrains出品的Clion配置Rust插件已经可以很好的支持Rust的开发了。一向对jetbrains很有好感的我决定再使用Clion开始新一期Rust学习。

安装mingw64

  1. mingw的下载托管在sourceforge,点击进入后选择x86_64-posix-seh进行下载(下载有些慢,还请耐心等待)。

  2. 此版本是一个免安装版本,可以直接解压在你想要的目录下(最好不要有中文)。

  3. 修改环境变量PATH,添加解压目录下的bin在这里插入图片描述

  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

在这里插入图片描述

  1. 打开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)
    
  2. 意思是首先要安装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
    
  3. 开始介绍了一下将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?
    
  4. 输入x86_64-pc-windows-gnu,表示我要安装64位的gnu版本

    Default toolchain? (stable/beta/nightly/none)
    
  5. 选择默认的工具链为stable,即稳定版本:

    Modify PATH variable? (y/n)
    
  6. 选择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
    
  7. 默认的安装选项已经被修改,此时输入1开始安装。安装过程中要从网络下载很多东西,请耐心等待。

  8. 安装完成后,在命令行窗口中输入rustc --version,输出:

    rustc 1.54.0 (a178d0322 2021-07-26)
    

    表示安装成功。

  9. 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!

安装Clion

Clion是Jetbrains出品的一款C/C++ IDE,它配合Rust的插件可以很好的支持Rust开发。Jetbrains出品,必属精品。可以直接在Jetbrains下载最新版本的Clion进行安装。

安装时,建议勾选创建桌面快捷方式和在右键菜单中添加将目录打开为项目的选项,以后用起来会非常方便。如下图:
在这里插入图片描述

安装完成,第一次启动时选择试用,需要登录Jetbrains账号,可以使用Github的第三方账号登录,然后点击开始试用即可。
在这里插入图片描述

在Clion弹出的启动界面上,选择插件,第一项就是Rust插件,点击安装。然后,根据zhile.io的指引,做一些你懂得的事情。

在这里插入图片描述

可以顺带着安装如Rainbow Brackets(使用不同的颜色标识不同层次的括号)、Chinese Simplified(汉化)等插件。全部安装完成后重启Clion。

使用Clion创建并调试项目

在启动界面中,点击New Project:
在这里插入图片描述

在左边选择Rust,然后在右边选择项目目录,工程模板选择Binary,点击创建按钮

在这里插入图片描述

然后在弹出的窗口中设置MinGW在选项,全部保持默认,点击OK就可以了

在这里插入图片描述

Clion生成的项目与刚才使用cargo创建的项目是一样的,修改一下代码,就可以进行调试了。

在这里插入图片描述

  • 12
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值