Lesson 1 — Hello World
Let me write 0x00 out front
Rust has become more and more popular in recent years, and I’m sure most experienced “siege lions” and “programming apes” have heard of it. If you haven’t heard it before, let’s learn it. If you have programming experience with Java, Kotlin, Python, Go, C++, JavaScript, C# and other high-level languages, it should be easy to learn Rust even if you don’t have any programming experience, because you can learn a language without being influenced by any language. I think it’s easier to get started.
0x01 What is Rust
The date of birth of Rust is 2006. It was originally the private project of Graydon Hoare, a Mozilla employee, and was first published in 2010. Hoare has since left Mozilla. If you want to know more about Hoare, you can check it out. Here’s an explanation for his departure from Mozilla — [strcat] : Rust (slash-r-slash-rust.github. IO)
Turning back, the sudden popularity of Rust certainly has its advantages. Rust has the advantages of memory security, memory management, ownership, type polymorphism, and more. In order to ensure memory security, a strict memory management model — an ownership system and a type system — is established. A strict compiler checks every variable and every memory pointer referenced in the code, establishing a clear lifecycle for all variables. If a variable goes out of life, it is automatically freed, so it does not require garbage collection as Java does. Each allocated memory has a pointer that has exclusive ownership. When the pointer is destroyed, the memory corresponding to the pointer is released.
0x02 Setting up a Rust environment
Declaration: All operations in this article are performed on Windows 10 systems. I can’t afford to buy fruit
Setting up an environment on a Windows operating system is relatively simple
1. Download the installation package from the official website
Install Rust – Rust Programming Language (rust-lang.org)
Select a 32-bit or 64-bit installation package based on your operating system.
2. Install Visual Studio (mandatory)
Install Visual Studio (mandatory)
Install Visual Studio (mandatory)
Rust requires the use of MSVC Build Tools. There is no separate installation package. The easiest way is to install Visual Studio
If you don’t do C# development, I recommend that you install Visual Studio 2013. Below is the download address.
Visual Studio Ultimate 2013 (x86) -DVD (Chinese-simplified) File name: cn_visual_studio_ultimate_2013_x86_DVD_3175316.iso SHA1: D6029A90916AA49F3F8F260C277DAF838DDA0612 file size: 2.87 GB release date: 2013-11-08 Emule: ed2k:// | file | cn_visual_studio_ultimate_2013_x86_dvd_3175316. Iso 3077509120 | | ADDA34B2BC29E1571276AE50A220EB91 | /Copy the code
3. Run rustc and press Enter. If the following information is displayed, Rust installation is complete.
0x03 Hello World
After all, it is the first step to learn every language
Create a random folder, create a file main.rs, and write the code shown above.
fn main() {
println!("hello world~~");
}
Copy the code
This file is then compiled using rustc.
Note: If you have compiled the error shown in the following image, you have not installed Visual Studio
After the compilation is successful, two more files are displayed in the directory.
Just click on Main.exe to run it… What happened? It was a flash. The reason is that the program executes too quickly and ends in a flash. Let’s pause the program by adding the following code
use std::process::Command;
fn main() {
println!("hello world~~");
// Command prompt pause
let _ = Command::new("cmd.exe").arg("/c").arg("pause").status();
}
Copy the code
Recompile and run again! It worked!! So this is the first Rust program that we wrote.
0 x04 summary
We first met Rust and wrote our first program, Hello World. We may not understand the syntax, but it works. That’s okay. We’ll talk more about that in a follow-up article. From the Hello World program, we also know that Rust prints information on a computer. Is it similar to C, Java, and Kotlin? Also, why do we have to install Visual Studio when building environments? Is there a way to run Rust without installing Visual Studio, as with Go? The answer is yes, you can baidu, I will not introduce here? Next section – Does Rust have any other build and package management tools?
0x05 Official Documents and Other Materials (free)
- Learn rust-Rust Programming Language (rust-lang.org)
- Rust tutorial | novice tutorial (runoob.com)
0x06 Source code for this section
001 StudyRust – Code Cloud – Source China (gitee.com)