On win10, configure the Rust development environment (using the mingw64 compiler) and idea to configure the Rust plugin

Article directory

    • 1. Preparation before installation
    • 2. Install mingw64
      • 2.1, the choice between the compiler mingw and visual studio
      • 2.2. Download
      • 2.3. Installation
      • 2.4. Configuration
      • 2.5. Test
    • 3. Install Rust
      • 3.1. Download [rustup-init](https://win.rustup.rs/) from Rust’s official website
      • 3.2. Configure the domestic mirror address
      • 3.3. Run rustup-init
      • 3.4. Verification
      • 3.5. Rustup common commands
    • 4. cargo configuration
      • 4.1. Configure cargo domestic image
      • 4.2. Cargo basic commands
    • 5. hello world
    • 6. Idea configures Rust plugin
      • 6.1. Download the Rust plugin
      • 6.2. Download source code
      • 6.3, view configuration
      • 6.4. Create a project
    • 7. Reference articles

1. Preparation before installation

2. Install mingw64

2.1, the choice between compiler mingw and visual studio

The bottom layer of Rust relies on the linker (connector) of C/C++, so the C/C++ compilation environment needs to be installed first.

There are two types of compilation chains for C/C++ on Windows:

  • Microsoft Visual Studio (msvc)

  • Mingw for GNU: https://www.mingw-w64.org/

If you use Visual Studio, you only need to install the C/C ++ compilation environment, and then install it by default all the way. The disadvantage is that **Visual Studio is relatively large, and it takes several G** to download and install. I choose to use mingw (mingw64) as the compilation chain for C/C++.

2.2, Download

mingw is divided into 32-bit and 64-bit, and this time I chose mingw64.

mingw is an open source project with many organizations participating.

For the installation operation in the mingw64 environment, see the download page https://www.mingw-w64.org/downloads/.

You can see that there are many ways to build mingw-64, and there are quite a few that support windows. I currently use the one marked by the circle in the picture above.

Mingw-builds builds

The address is: https://github.com/niXman/mingw-builds-binaries/releases

Select x86_64-12.2.0-release-posix-seh-rt_v10-rev0.7z to download.

This compressed package is only a little over 67MB, which saves hours and storage space compared to installing msvc.

Differences between various versions of mingw

OS version:

  • x86_64: 64-bit version
  • i686: 32-bit version

Threading model:

  • posix: Compared with win32, posix has better support for the standard library of C++11.
  • win32:

If you want to perform breakpoint debugging during code development, it is recommended to use posix.

Exception handling method:

  • seh: 64-bit system is available, using the exception handling mechanism of windows itself.
  • dwarf: only supports 32-bit, additional debugging information needs to be added to the executable program, and the program size is large.
  • sjlj: Much slower than other exception handling.

2.3, installation

The downloaded x86_64-12.2.0-release-posix-seh-rt_v10-rev0.7z is a installation-free version, which can be directly decompressed in the directory you want (The directory name should not contain Chinese).

2.4, Configuration

Modify the environment variable path and add bin in the decompression directory.

2.5, test

Open the cmd window, enter gcc --version, if the following content is displayed, the configuration is successful:

3. Install Rust

3.1, download rustup-init from Rust’s official website

3.2, configure the domestic mirror address

rustup-init is an online installation tool. The default installation source is a foreign site, and the network speed is slow. You can set the environment variable to download it from the domestic mirror.

Add the following variables to the system environment variables (you can choose one of the two):

### The following configurations can be selected from one of the two

# University of Science and Technology of China
RUSTUP_DIST_SERVER=https://mirrors.ustc.edu.cn/rust-static
RUSTUP_UPDATE_ROOT=https://mirrors.ustc.edu.cn/rust-static/rustup

# Tsinghua University
RUSTUP_DIST_SERVER=https://mirrors.tuna.tsinghua.edu.cn/rustup
RUSTUP_UPDATE_ROOT=https://mirrors.tuna.tsinghua.edu.cn/rustup/rustup

3.3, run rustup-init

Open the cmd window and run rustup-init.exe , the following content is displayed:

The picture above is a compiling environment that requires the installation of C/C++. The default is the visual studio installer, and we use mingw64, so it needs to be modified manually.

Here, either 2 or 3 can be selected.

enter

2

enter

y

Enter 2 for a custom installation.

2

Enter x86_64-pc-windows-gnu , indicating that I want to install the 64-bit version of gnu.

x86_64-pc-windows-gnu

The next step is to press Enter, using the default configuration.

After the above configuration is completed, press Enter at this step to start the installation.

The installation process is shown in the figure below. During the installation process, a lot of things will be downloaded from the Internet, please wait patiently.

3.4, Verification

After the installation is complete, reopen the dos command line window, enter rustc --version, and output

C:\Users\admin>rustc --version
rustc 1.68.1 (8460ca823 2023-03-20)

Indicates that the installation was successful.

3.5, rustup common commands

# Check rustc version
rustc --version

# Display information about the currently installed toolchain
rustup show
# Check for updates
rustup update
# uninstall
rustup self uninstall
# Set the current default toolchain
rustup default stable-x86_64-pc-windows-gnu
# view help
rustup -h

# --------------------------> Configure Toolchain
# View toolchain
rustup toolchain list
# Install the toolchain
rustup toolchain install stable-x86_64-pc-windows-gnu
# Uninstall the toolchain
rustup toolchain uninstall stable-x86_64-pc-windows-gnu
# Set up a custom toolchain
rustup toolchain link <toolchain-name> "<toolchain-path>"

# -------------------------->Configure the default toolchain for a directory and its subdirectories
# View the default toolchain that has been set
rustup override list
# Set the default toolchain for this directory and its subdirectories
rustup override set <toolchain> --path <path>
# Cancel the default toolchain for the directory and its subdirectories
rustup override unset --path <path>

# --------------------------> Configure available targets for the toolchain
# View target list
rustup target list
# install target
rustup target add <target>
# uninstall target
rustup target remove <target>
# Install targets for a specific toolchain
rustup target add --toolchain <toolchain> <target>

# -------------------------->Configure components installed by rustup
# View available components
rustup component list
# Install components
rustup component add <component>
# Uninstall the component
rustup component remove <component>

4. cargo configuration

cargo is a package management tool for rust, similar to python’s pip. The default cargo will download packages from foreign websites, and it can also be modified to download from HKUST.

4.1. Configure cargo domestic image

Create a new file under the .cargo directory of the user’s home directory, name it config (without the extension), and enter the following content:

[source. crates-io]
registry = "https://github.com/rust-lang/crates.io-index"
replace-with = 'ustc'

## Two choose one
## replace-with = 'XXX' select configuration

# University of Science and Technology of China
[source.ustc]
registry = "git://mirrors.ustc.edu.cn/crates.io-index"

# Tsinghua University
[source.tuna]
registry = "https://mirrors.tuna.tsinghua.edu.cn/git/crates.io-index.git"

4.2, cargo basic commands

 View cargo version
cargo --version

# New Project
cargo new <project_name>

# Build the project
cargo build

# run the project
cargo run

# Check item
cargo check

# view help
cargo -h

5, hello world

After a series of installation and configuration, the rust operating environment is configured, and a hello world is written below.

We use cargo to create a project, enter cargo new hello --bin in the cmd window,

cargo new hello --bin

cargo has created the hello project for us and automatically generated a series of files:

hello
└── hello
   ├──.gitignore
   ├── Cargo.toml
   └── src
        └── main.rs

Among them, main.rs under src is a rust code file, and its content is:

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

Run this code:

cd hello

cargo run
   Compiling hello v0.1.0 (C:\Users\admin\Desktop\tt\hello)
    Finished dev [unoptimized + debuginfo] target(s) in 15.61s
     Running `target\debug\hello.exe`
Hello, world!

The complete operation record above:

6. Idea configuration Rust plugin

6.1, download the Rust plugin

6.2, download source code

Enter the command directly in cmd to download the source code (idea will automatically find the path)

rustup component add rust-src

6.3, view configuration

6.4, create a project

1) Select Rust, other defaults, click Next.

2) Select the path and enter the project name: rust_demo1 .

3) The project is created successfully, as shown in the figure below.

7. Reference articles

https://blog.csdn.net/zhmh326/article/details/103805485

https://zhuanlan.zhihu.com/p/492412918?utm_id=0

https://www.mingw-w64.org/

https://www.cnblogs.com/lixueren-wy/articles/16934078.html

https://blog.csdn.net/saibeifeng187/article/details/107567702