This is the second day of my participation in Gwen Challenge.
Rust is a language that empowers everyone to build reliable and efficient software.
All the following examples and codes are based on the Mac environment, the editor is vscode, if you encounter any problems, welcome to join the exchange group, exchange together.
Getting started: Hello World
Whenever we learn a new language, we always start by printing out a Hello World. So Rust is no exception.
Create a new project
Create a rust_projects and subdirectory hello_world and open it with vscode
mkdir rust_projects
cdRust_projects code. // a shortcut to open with VScode, just use vscode to open itcd hello_world
Copy the code
Creating a Rust File
Rust files end with. Rs and their naming convention is to split them with underscores. For example, to create a helloWorld file, it should be named hello_world.rs, not helloWorld.rs or helloWorld.rs. Create a new hello_world.rs file:
touch hello_world.rs
Copy the code
New entry main function, print Hello world.
fn main() {
println!("Hello, world!");
}
Copy the code
How to run it? With nodejs, you only need Node xxx.js, but Rust requires compilation because JavaScript is compiled in the host environment on the fly, so it doesn’t feel compiled. So we need to compile the Rust file we just wrote.
rustc main.rs
./main
Copy the code
After executing the above script, the terminal output Hello World, which is successful. Congratulations, you have completed a Rust application. Let’s play a little game to understand Rust better.
Rust package Manager VS NPM
Before starting the game, it’s important to understand the Rust package manager, because mini-games require additional dependencies. NPM is the NodeJS package manager and Cargo is Rust’s build system and package manager. Most Rust developers use Cargo to manage their Rust projects because it handles many tasks for you, such as building code, downloading dependent libraries, and compiling them. Cargo is part of the rust built-in because the Rust environment is already installed. Hurry to check whether the installation is successful. If that doesn’t work, you can uninstall and reinstall the portal.
Cargo --version or cargo -V output cargo X.Y.Z (XXXXXXX YYYY-MM-DD) is successfulCopy the code
Let’s initialize a project with Cargo. The difference is that NPM will only create package.json for you. Cargo will create folders and related files for you.
cd. / cargo new guessing_game in rust_projects is equivalent to NPM init -ycd guessing_game
Copy the code
The directory structure is
. ├ ─ ─ Cargo. Lock with package - lock. / / json equivalent ├ ─ ─ Cargo. Toml / / with the package. The json equivalent ├ ─ ─ the SRC │ └ ─ ─ the main, the rs / / entry documents └ ─ ─ the target / / ignoreCopy the code
In this case, Cargo. Toml is similar to package.json, with project name, version number, dependencies, and so on. Cargo can also help us package, distribute, and Rust Package warehouse addresses. Detailed functions will be introduced later.
[package]
name = "guessing_game" // package.json name
version = "0.1.0 from" // package.json version
authors = [""] // package.json authors
edition = "2018"// Switch the version used by Rust, which can be deleted manually[dependencies] // package.json dependencies
Copy the code
At the end
Next section: Guess the numbers game
Previous section: Rust from Start to Run – Part 1 Environmental preparation