Rust lang Tips andTricks

栏目: IT技术 · 发布时间: 4年前

内容简介:Rust is a programming language that is geared towards speed and safety. Rust has gained a lot of adoption in the past few years and developers are loving it. Although there are numerous other systems programming languages, Rust is one of the very few alter

Rust is a programming language that is geared towards speed and safety. Rust has gained a lot of adoption in the past few years and developers are loving it. Although there are numerous other systems programming languages, Rust is one of the very few alternatives to C/C++ that can hold its own when it comes to performance.

Rust lang Tips andTricks

Rust is the language of our choice at Polymath for our upcoming Blockchain, Polymesh. In this post, I’ll be sharing some of my favorite Rust tricks and some tips for the new developers.

Enum size is bounded by the largest member

Enums are sized to be able to hold their largest variant. Therefore, it’s recommended to have similar sized variants within enums to avoid having a nonoptimal memory layout. You can consider boxing the larger variants if required. Consider this example:

enum Foo{
    A(u64),
    B([u64; 1000]),
}

enum FooBoxing {
    A(u64),
    B(Box<[u64;1000]>)
}

fn main() {
    let x = Foo::A(0); // Its size is 8008 bytes.
    let y = FooBoxing::A(0); // Its size is just 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 A of enum Foo is much smaller in size than variant B but the memory layout used for both variants will be the same and hence when variant A is used, it will have nonoptimal performance.

Avoid unnecessary clones

Calling .clone() on a variable creates a copy of their data and it takes resources to create a copy. Therefore, clones should be avoided wherever possible. Often times, you will be able to pass references of the same variables to different functions rather than needing to clone them. For example:

fn main() {
    let x = Foo::new();
    func(x.clone());
    func(x.clone()); //This clone is not needed
}

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
}

Modularizing tests

If you structure your tests like

tests/   
    foo.rs   
    bar.rs

Then each one of those tests gets compiled as a separate binary, and that takes more compilation time and space. You can instead add your test files as modules to a single test so that only a single binary is generated. Your new tests structure will look something like:

tests/
   all/
     mod.rs // mod foo; mod bar;
     foo.rs
     bar.rs
   mod.rs // mod all;

We were able to cut our CI test time from 26 minutes to 8 minutes using this trick. It has the disadvantage that you can not change one test file and compile just that file. It will always compile the full binary even if you have changed just one test file. For us, compiling individual files was only ~10 seconds faster than compiling the full binary and hence we decided to go with this approach.

The dbg! macro

The dbg macro can be used to print the value as well as the source code of an express to stderr. Example usage:

let a = 2;
let b = dbg!(a * 2) + 1;

The above code will print:

[src/main.rs:2] a * 2 = 4

Using _ to make large numbers legible

In Rust, you can use _ between numbers to make them easier to understand.

let answer = 42000000 // ugly
let answer = 42_000_000 // beautiful

The standard swap function

The swap function allows you to directly swap two variables without needing to create 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);

? converts between error types

As you probably already know, ? can be imagined as unwrap that returns the error instead of panicking. Instead of directly returning the error,  ? actually returns Err(From::from(err)) . That means the error is automatically converted to the proper type if it is convertible.

Same name macro, function, and type

It is possible to declare a macro, a function and a type like an Enum with the same name and then import all three of them elsewhere with a single import statement.

sccache

sccache can cache cargo build artifacts so that they can be reused across workspaces. This means that if you have multiple Rust projects and they use an identical dependency, sccache will allow you to compile that dependency once and then reuse across the projects. It will save you compilation time and disk space. Rust target dirs are already so big, there’s no reason to store redundant binaries there.

Rust lang Tips andTricks

You can install sccache with Cargo: cargo install sccache . To enable sccache , you need to add RUSTC_WRAPPER=sccache to your build environment. One way to do that is to add export RUSTC_WRAPPER=sccache to your  .bashrc .

Clippy and rustfmt

They are two of my favorite Rust tools and if you don’t already use them, give them a try. Clippy can catch a variety of lints in your code and helps you write idiomatic code. To install Clippy, run rustup component add clippy and to run Clippy in your workspace, execute cargo clippy . More information can be found on Clippy’s GitHub .

rustfmt, as the name suggests, is a tool for formatting Rust code according to style guidelines. To install rustfmt, run rustup component add rustfmt and to run rustfmt in your workspace, execute cargo fmt . More information can be found on rustfmt’s GitHub .

I’d also like to give a shoutout to rust-analyzer . It is an experimental modular compiler frontend for the Rust language. It works better than any other Rust compiler frontend for me. I highly recommend everyone to try it out.

Conclusion

Rust has a lot to offer and you can learn something new about Rust every day. I hope you learned something new from this post. If you want to add something or need help, feel free to leave a comment or reach out to me via other mediums.

Happy Hacking!


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

自媒体写作,从基本功到实战方法

自媒体写作,从基本功到实战方法

余老诗 / 清华大学出版社 / 2018-9-1 / 59.00元

《自媒体写作》是一本系统而通俗易懂的自媒体写作指导书。 全书共分为10章,分别从写作基本功、新媒体认知、新媒体传播规律和自媒体作者阅读写作素养以及如何进阶等方面展开,结合简书、公众号、今日头条等主流自媒体所选例文,讲解写作知识和新媒体特点,内容详实,有理有据,非常适合自媒体写作爱好者自学。 尤其值得一提的是,写作基本功部分从原理、方法和技巧三个层面展开论说,让自媒体写作学习者既能从根本......一起来看看 《自媒体写作,从基本功到实战方法》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码