In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what is the use of Rust language skills". In daily operation, I believe many people have doubts about the use of Rust language skills. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "what is the use of Ruth language skills?" Next, please follow the editor to study!
Racer,Clippy,rustfmt and fix
This is a very useful set of tools.
Racer is used to help you complete the rust code.
You can use cargo to install racer using:
Cargo install racer
Racer needs to obtain the Rust source code path first, and you can obtain the source code through rustup:
Rustup component add rust-src
Rustup update can get the latest code at any time.
Clippy can capture all kinds of lints in the code and check whether the code conforms to the rust idiom and whether it is authentic, efficient or not.
To install Clippy, run rustup component add clippy
Then run Clippy in the workspace:
Cargo clippy-tests-W clippy::cargo
You can determine what to check for clippy by executing command-line arguments or clippy.toml configuration documents:
Rustfmt is a tool for formatting Rust code according to style.
To install rustfmt, use:
Rustup component add rustfmt
Then run rustfmt in the workspace:
Cargo fmt .
Similarly, you can use rustfmt.toml to configure the style of rustfmt:
The cargo fix tool automatically fixes warning entries in the compiler.
Macros, functions and types with the same name
Those of you who are familiar with Rust may know that one of the most powerful features of Rust is macros, such as in the Hello,Chongchong example:
Fn main () {println! ("Hello,Chongchong");}
Println! () is just a macro.
In Rust we can declare a macro, a function, and a type with the same name, and then use an import statement to introduce them into other files.
Dbg! Macro
DBG macros can be used to display the evaluation of expressions and values, and can be used to quickly debug parts of code expressions. For example:
Let a = 2; et b = dbg! ((aq3) * 2) + 1; assert_eq! (b, 11)
The above code will print out:
[dbg.rs:3] (a + 3) * 2 = 10
Error type conversion
Error handling using rust directly may not be flexible enough, using unwrap () will trigger panic when an error occurs, causing the program to hang up, which in many cases is not what we expect. Here is an example
Fn main () {let path = "/ tmp/example"; println! ("{}", read_file (path));} fn read_file (path: & str)-> String {std::fs::read_to_string (path). Unwrap ()}
If / tmp/example does not exist, a panic error will be triggered:
Thread 'main' panicked at' called `Result::unwrap () `on an `Err` value: Os {code: 2, kind: NotFound, message: "No such file or directory"}', ok.rs:7:5 note: run with `Err1` environment variable to display a backtrace
It's a good thing we can use it. Yes? Operator can convert error returns to Err (From::from (err)) and Ok (ok) branch processing, so that errors can be converted to automatic types. Part of the code is as follows. You can customize the error message CustomError:
Fn main ()-> std::result::Result {let path = "/ tmp/example"; let v = read_file (path)?; Ok ()}
Modular testing
Imagine that the test structure of your project is as follows:
Tests/ aaa.rs bbb.rs
During testing, each of these is compiled into separate binaries, which takes a lot of time and space to compile. You can add these test files as modules to a test so that only one binary is generated. The new test structure is as follows:
Tests/ all/ mod.rs / / mod aaa; mod bbb; aaa.rs bbb.rs mod.rs / / mod all
Using this technique, the compilation time can be greatly reduced, time and space can be saved and efficiency can be improved.
Of course, it also has the disadvantage that it cannot test a single file, but can only compile the file uniformly. Even if only one test file is changed, it compiles the complete project file.
Compiler cache
The Rust compiler cargo only supports compilation caching between projects within workspaces, not between workspaces. If you use the same dependency for multiple projects in multiple workspaces, it takes extra time to compile separately. We can solve this problem with the cargo caching tool sccahe.
Sccache is a compiler cache similar to ccache's cargo, which compiles the dependency once and can then be reused in all projects. It can greatly save compilation time and disk space. In addition to being able to store compiled builds locally, sccache also supports storage in the cloud, such as AWS S3 or GCS.
You can use the operating system package manager or cargo to install sccache:
Cargo install sccache .
Can be used under windows
Scoop install sccache
To compile using sccache cache rust, you need to define build.rustc-wrapper in the cargo configuration file (~ / .cache / config):
[build] rustc-wrapper = "/ path/to/sccache"
You can also set the RUSTC_WRAPPER environment variable directly at compile time:
RUSTC_WRAPPER=/path/to/sccache cargo build
Avoid unnecessary cloning
Calling .clone () on a variable in rust creates a copy of the data for it. Creating a copy of the data consumes a lot of memory resources, so in most cases it will affect the performance of the program and should be avoided. In general, you can use an application reference instead of creating a clone. For example:
Fn main () {let x = Foo::new (); func (x.clone ()); func (x.clone ()); / / the clone is a non-essential fn main () {let x = Foo::new (); func (x.clone ()); func (x); / / This will work fine because you do not need / / to use x after this call}
Use:
Fn main () {let x = Foo::new (); func (& x); func (& x);}
Enumeration size is limited by maximum member
The size of the enumeration allows it to accommodate its largest variation. Therefore, it is recommended that you use a variant of a similar size within the enumeration to avoid a poor memory layout. If necessary, consider Boxizing a larger variable. Consider the following example:
Enum Foo {A (U64), B ([u64; 1000]),} enum FooBoxing {A (U64), B (Box)} fn main () {let x = Foo::A (0); / / size 8008 bytes let y = FooBoxing::A (0); / / 16 bytes println! ("Foo size {:?}", std::mem::size_of_val (& x)) Println! ("FooBoxing size {:?}", std::mem::size_of_val (& y);}
In the above example, variant An of enumerated Foo is much smaller than variant B, but the memory layout for both variants will be the same, so its performance will not be ideal when using variant A.
Standard switching function
The rust swap function allows you to exchange two variables directly without creating a temporary variable.
Use std::mem; let mut x = 5; let mut y = 42; mem::swap (& mut x, & mut y); assert_eq! (42, x); assert_eq! (5, y); at this point, the study of "what is the use of Rust language skills" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.