[This is my 15th day of the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021]
How does Rust make programmers better
The essence of Rust programming language is Empowerment, which enhances coding through strict constraints. Rust allows you to move effortlessly into “systems-level” work, getting into the low-level details of memory management, data presentation, and concurrency. In Rust, the compiler acts as a gatekeeper, refusing to compile code with these hard-to-catch bugs, including concurrency bugs. By working with the compiler, the team spent more time focusing on program logic rather than bug tracking.
For ease of use, Rust provides tools to help developers write code. As follows:
Rustfmt
This ensures that developers follow a consistent code style.Cargo
Is a built-in dependency manager and build tool that adds, compiles, and manages dependencies and makes them have the same file structure in the Rust ecosystem.Rust Language Server
Provides powerful code completion and inline error message capabilities for integrated development environments (ides).
Rustfmt
Rustfmt is an automatic formatting tool that formats code according to the community code style, and many projects use Rustfmt to avoid arguments about writing rust-style code.
If you want to stick to a standard style in Rust projects, you also need to format your code in a specific style using Rustfmt. Several common format constraints are shown below
- Rust indents four Spaces instead of one TAB;
println!
Invoke a Rust macro. If it calls a function instead, the input is println (without!).- Most lines of Rust code end with a semicolon.
Installation:
rustup component add rustfmt
Copy the code
Format command:
cargo fmt
Copy the code
Running this command formats all Rust code in the current Crate. This should only change the code style, not the code semantics.
Cargo
Cargo is Rust’s build system and package manager and can be used to build code, download, and compile dependent libraries.
Relevant command
Cargo build cargo Build cargo Run Cargo Run cargo Cargo check --release builds cargo Build --release instead of producing the executableCopy the code
Release generates executables under target/release instead of target/debug. These optimizations make Rust code run faster, but they also take longer to compile.
Create a project
$ cargo new hello_
Created binary (application) `hello_` package
Copy the code
Directory structure:
- .git: git repository
- src
- main.rs
- Cargo. Toml: Uses the TOML (Tom’s Obvious, Minimal Language) format, which is Cargo’s configuration file
The default contents of the Cargo. Toml file:
[package] name = "hello_" version = "0.1.0" authors = ["name <email>"] edition = "2018 definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies]Copy the code
- Package indicates that the following statements are used to configure the package;
- Dependencies Lists the dependencies of a project to which new dependencies are added
[dependencies]
Under.
Write and run Rust programs
The project generates a source file named main.rs. Rust source files always end with the.rs extension. If the filename contains more than one word, separate them with an underscore. For example, call it hello_world.rs instead of helloWorld.rs.
fn main() {
println!("Hello, world!");
}
Copy the code
main
A function is a special function: it is always the first code to run in an executable Rust program. The function body is wrapped in curly braces{}
In the. Rust requires that all function bodies be wrapped in curly braces.- println! A Rust macro was invoked. If you are calling a function, type println (no need!) ;
- The line ends with a semicolon (;). , which represents the end of one expression and the beginning of the next.
Save the file and return to the terminal window. On Linux or macOS, type the following command to compile and run the file:
$ rustc main.rs
$ ./main
Hello, world!
window:
> .\main.exe
Copy the code
Rust Language Server (RLS)
RLS provides a server that runs in the background and provides information about Rust programs to ides, editors, and other tools.
The installation
Rustup Update rustup component add RLS rust-analysis rust-srcCopy the code
Install VScode-rust to check whether the RLS configuration is successful.
You can see that the syntax hint already exists in the figure below
The resources
hello cargo