I’m participating in nuggets Creators Camp # 4, click here to learn more and learn together!

Dear, hello, everyone. I am “Front-end Xiaoxin”. 😇 has been engaged in front-end development and Android development for a long time


Recently, I looked at the development of desktop applications. After watching the application of Electron, I learned a more novel Tauri framework, which uses Rust internally. Meanwhile, I found that some training institutions are also following the trend of Rust’s drainage courses. So it’s important to understand and learn the basics of Rust in order to support subsequent learning of Tauri.

Quick Configuration environment (Win11, 64-bit) :

  1. Install the Microsoft C++ build tool: download it and double-click to install it. Note that you need to restart the computer and install the version 2019. If the build tool 2017 is installed on the computer, uninstall it first.

  2. Install the 64-bit Rustup tool: Double-click the Rustup tool again to start the installation. After the installation, you are advised to configure C:\Users\ username \.cargo\bin in the Path of the environment variable for direct use by terminal commands.

  3. To check whether the installation is successful, we usually use the command to check the version as the installation is normal. Here we check the cargo version and the RUSTC version.

    1. Cargo Version check: cargo –version

    2. Rustc version check: ruSTC –version

Know the Cargo:

Cargo was installed by default when we used the Rustup installation. Cargo is a tool for building and managing dependency packages. Let’s list some common commands for Cargo.

The serial number Terminal command function
1 cargo new Create the project and initialize the Git repository
2 cargo build Build project (debug), add –release release build
3 cargo run Compile project (not compiled or source code modified) & run project
4 cargo check Just check the code to make sure it compiles faster than build
6 cargo publish Publish custom libraries

We can find the third-party libraries we need in crates. IO, and we can publish our own custom libraries to crates.

Preparation before writing code:

  1. VSCode related plug-ins

  2. IntelliJ IDEA plug-ins

    Note: These are the two editors I use most often, although there are others that have plug-in support for accessing Rust documents.

Create a new project using Cargo:

  1. To create a new project using cargo New Hello-rust, here is the generated directory structure;

    hello-rust
    |- Cargo.toml
    |- src
      |- main.rs
    Copy the code
  2. So if you run cargo run and you see it compiled and executed to output on the terminal, you’re familiar with Hello, World! .

    Note: the Cargo. Toml file records the project name, version number, Rust version and dependency configuration, which is equivalent to package.json in NodeJs development and Androidmanifest.xml in Android development.

Rust example program:

  1. Learn how to import dependencies;
  2. Learn how to use output statements;
  3. Understand what mutable variables are;
  4. Understand what correlation functions are;
  5. know&Get address character;

Code details:

// Use the use keyword to import standard/other libraries
use std::io;

fn main () {/ /! It means macros are different from functions
    println!("Please enter a number.");
    
    // Use mut to declare variable variables. There is no way to assign values twice to variables defined by let
    // :: indicates that new is an associative function of String, similar to static methods in Java or other languages
    let mut guess = String::new();

    // Read terminal input by importing IO module in standard library
    // expect: executes on program exceptions
    // & GUESS: indicates that we are using a reference to GUESS here, and they point to the same space in memory
    // &mut: indicates that the received reference here is also mutable
    io::stdin().read_line(&mut guess).expect("Read failed");

    // Print guess with the {} placeholder
    println!("The numbers entered are: {}", guess);
}
Copy the code

Terminal preview:

Note: You will see in our second executioncargo runThe output of the Compiling information is not available because the source code has not been modified when we are running again, reducing compilation to speed up the execution.

Install and use third-party dependencies:

  1. Search for dependencies in crates. IO.
  2. Will need to install dependencies according toDependency name = versionFormat configuration toCargo.tomlIn the file[dependencies]Below;
  3. Run after changing the configuration filecargo buildThe installation will be performed, and of course the VSCode plugin will automatically complete the installation and automatically update the Cargo. Lock file to lock the dependent detailed version,Please do notManual change.

Install rand library to generate random numbers:

  1. willRand = "0.8.4"Configuration to[dependencies]Wait for the plug-in to install or execute manuallycargo build;
  2. Import the rand libraryuse rand::Rng;
  3. Write random number generation code:
    fn main() {
        let mut rng = rand::thread_rng();
        // Random number generation from 1 to 100
        let random_num = rng.gen_range(1.101);
        println!("Generate random number: {}", random_num)
    }
    Copy the code

Examples in the documentation (Rust – random. Making. IO/book/guide -…) :

Conclusion:

At first glance, the syntax of Rust is quite different from that of other programming languages. However, the use of the & address is more clear and verbose. In the future, I will write some examples of the syntax of Rust to give an overall experience of the design of this efficient and reliable programming language.


Welcome to follow my public account “Front-end Xiaoxin students”, the first time to push original technical articles.