YouTip LogoYouTip

Rust Setup

Rust Environment Setup

Rust supports many integrated development environments (IDEs) or text editors dedicated to development.

The official website lists the supported tools as follows (https://www.rust-lang.org/zh-CN/tools):

Image 1

This tutorial will use Visual Studio Code as our development environment (Eclipse has a version dedicated to Rust development, which is also a good choice for beginners).

Note: IntelliJ IDEA is difficult to debug after installing the plugin, so it is recommended that developers accustomed to using IDEA use CLion, but CLion is not free.

Setting up the Visual Studio Code Development Environment

First, you need to install the latest version of the Rust compiler toolchain and Visual Studio Code.

Rust compiler toolchain: https://www.rust-lang.org/zh-CN/tools/install

Visual Studio Code: https://code.visualstudio.com/Download

The Rust compiler toolchain depends on the C language compiler toolchain, which means that your computer must already have a C language compilation environment. If you are using a Linux system, you often already have GCC or clang. If you are using macOS, you need to install Xcode. If you are using the Windows operating system, you need to install Visual Studio 2013 or later (with C/C++ support) to use MSVC, or install MinGW + GCC compilation environment (Cygwin has not been tested).

Installing the Rust Compiler Toolchain

The Rust compiler toolchain can be downloaded from the official website: https://www.rust-lang.org/zh-CN/tools/install.

For macOS, Linux, or other Unix-like systems, to download Rustup and install Rust, run the following command in the terminal:

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

For Windows, download the rustup-init.exe executable file.

The downloaded Rustup on Windows is an executable program named rustup-init.exe.

Now execute the rustup-init file:

Image 2

The image above shows a command-line installation wizard.

If you have already installed MSVC (recommended), then the installation process will be very simple. Enter 1 and press Enter to proceed directly to the second step.

If you installed MinGW, then you need to enter 2 (custom installation), and then the system will ask you "Default host triple?". Please change the "msvc" in the default host triple shown in the image above to "gnu" and then enter the installer:

Image 3

Keep all other properties as default.

After setting all options, you will return to the installation wizard interface (the first image). At this point, we enter 1 and press Enter.

Image 4

At this step, the Rust installation is complete. You can test it with the following command:

rustc -V # Note the uppercase V

Image 5

If the above two commands can output the version number you installed, then the installation was successful.

For more download methods, please refer to: https://forge.rust-lang.org/infra/other-installation-methods.html

Setting up the Visual Studio Code Development Environment

After downloading the Visual Studio Code installer, launch the installation wizard to install it (this step is not repeated here).

After installing Visual Studio Code (hereinafter referred to as VSCode), run VSCode.

Image 6

Find "Extensions" in the left sidebar, search for "Chinese", and install the Simplified Chinese extension to change the interface to Chinese. (If you prefer to use the English interface or your computer does not support Chinese characters, this step can be skipped).

Image 7

Use the same method to install the rust-analyzer and Native Debug extensions.

Image 8

Image 9

Restart VSCode, and the Rust development environment is set up.

Now create a new folder, such as -greeting.

Image 10

Open the newly created folder in VSCode:

Image 11

After opening the folder, select "Terminal" - "New Terminal" from the menu bar, and a new terminal will open:

Image 12

Enter the following command in the terminal:

cargo new greeting

A Rust project directory named greeting will be created in the current folder.

Image 13

Now enter the following three commands in the terminal:

cd ./greeting
cargo build
cargo run

The system will generate a Hello, world source program main.rs when creating the project, which will be compiled and run:

Image 14

At this point, you have successfully built a Rust command-line program!

For information on debugging programs in VSCode, please refer to the Cargo Tutorial.

← Os ChdirFunc String Mb_Strlen β†’