RUST Learning Diary Lesson 3 – IDE


0 x00 review

In the previous section we learned about Cargo and some of its common commands. In a production environment, we typically use Cargo to build projects. Especially for complicated programs. In the following sections, we will use Cargo to build the project for all of our code.

0x01 Visual Studio Code

Visual Studio Code is a powerful, free, cross-platform editor from Microsoft with a wealth of plug-ins.

Download and open Visual Studio Code and search for the extension rust

Let’s install the first one. Wait until the installation is complete and restart Visual Studio Code. Then create or open a new folder, I have created and opened a folder D:/StudyRust/003, then run the create project command in terminal, it will create a Rust project in our directory.

We have two ways to execute this project:

  • The terminal executioncargo run
  • Right-click VS Code Workspace – Run Code (ruSTC by default, not recommended)

I won’t go into the execution process and the result, just like we execute the command on the command line. If you need to Debug Rust, you’ll also need to download Native Debug, which I won’t cover here.

PS: Rust also has a handy plugin called Rust-Analyzer, which is said to be even more useful than this. You can compare, the specific installation method can be baidu ~

0x02 CLion or IDEA

I don’t need too much introduction of IDEA, generally students who have done development know this software, IDEA can choose the community version (free). CLion is a special C++ IDE based on IDEA, but this software is charged, conditional students can use. Here I take CLion as an example to demonstrate the same operation of IDEA.

Install and open CLion, install the Rust plug-in, and restart.

Create a new project by default. Location in the first line can change the Location of the project, and the rest of the defaults can be used. Create a project for the first time, can choose toolchain popups, if you want to change the toolchain, can through the File – Settings – Build, Execution, Deployment, changes in the toolchains.

I chose MSVC, which stands for Visual Studio 2013, but you can also choose MinGW. So the answer to the question from the first section is that I can write Rust without installing Visual Studio.

Click the green triangle symbol in the upper right corner to run the code, the little turtle is the debugging code, about the debugging program, will be introduced below. The Run workspace below is the result of running the code and you can see that Hello World! Has been printed. .

0x03 How to Select an IDE

He that will do his work well must first sharpen his tools. There are more than two tools to write Rust. Eclipse, Visual Studio, etc., all have their own plug-ins for developing Rust. In my opinion, if you just want to learn Rust, Visual Studio Code is enough, and if you want to go further, you can choose a professional IDE. Of course, I am not saying that Visual Studio Code can not do professional development, in fact, it can, but also configure the Debug tool, I am lazy, like once and for all, in order to facilitate the subsequent program debugging and personal use habits, the following article will take CLion as an example to demonstrate. Choose an IDE that you are comfortable with and don’t mind others’ comments.

0x04 Debugger

Understand the code that appears below, which will be covered in a later section. This is just to demonstrate Debug.

fn main() {
    let a = 5;
    let b = 6;
    let c = 7;
    let x = a + b + c;
    println!("a + b + c = {}", x);
    println!("Hello, world!");
}
Copy the code

Example procedures: find the sum of a + b + C. At the beginning of each line of code, we can click on a breakpoint. Then click on the turtle in the upper right corner.

The broken arrow in the lower left corner represents single-step execution (not going into the underlying code), and the arrow to the right is also single-step debugging, which is single-step execution into Rust’s source code. The following three commands are also common debugging commands, you can feel the difference. I don’t have much to say here.

In the red box on the right below, the value of each variable is displayed. You can easily view the value of each variable. And right click on each variable to do something with it, you can modify the value, copy the value and so on. Next, the LLDB.

The first button is to execute to the next breakpoint, the second button is to pause, the third button is to stop running the program, the fourth button is to view all breakpoints for the current project, and the fifth button is to close all breakpoints. Closing does not mean clearing, but only invalidates all breakpoints of the program.

For those of you who use Xcode a lot, you should know LLDB, which is easy to debug. As shown in the picture above, I first printed the variable of A with print a, and then changed the value of a to 42 with expression a = 42.

PS: If you want to know more about LLDB, please go to Baidu. I will not introduce more about it here

0x05 This section source code

003 · StudyRust – Code Cloud – Open Source China (gitee.com)

Next up — learn about some of Rust’s commonly used specifications.