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!


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

查看所有标签

猜你喜欢:

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

Web信息架构(第3版)

Web信息架构(第3版)

[美] Peter Morville、Louis Rosenfeld / 陈建勋 / 电子工业出版社 / 2013-10 / 99.00元

本书内容涵盖了信息架构基本原理和实践应用的方方面面。全书共7个部分,包括信息架构概述、信息架构的基本原理、信息架构的开发流程和方法论、信息架构实践、信息架构与组织、两个案例研究,以及参考资料清单。 本书兼具较高的理论价值和实用价值,曾被Web设计领域多本书籍重点推荐,是信息架构领域公认的经典书籍,不论新手还是专家都能各取所需。本书可供Web设计与开发者、Web架构师、网站管理者及信息管理相关......一起来看看 《Web信息架构(第3版)》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具