No more run time !! Enjoy Compile time function evaluation using `const fn` in Rust

Table of contents
Reading Time: 2 minutes

Sometimes we want the compiler to pre-compute values at compile time. If you have ever done programming in C++, you would have heard about constexpr which is used to improve performance by doing computations at compile time rather than run time.

A few days ago, I was exploring Rust’s Unstable Book and found pretty much same feature in Rust, which is const_fn. I started exploring this feature more after the recent Rust release 1.33.0, in which Rust team has announced major improvements in const fn. The idea of using const fn is to compute result at compile time so that time can be saved when code is run.

const fn is a pure function, which means the return value does not depend on anything but the function parameters. For same input, it will always return same output. Let’s see a small example:-

const fn square_area(a: i32) -> i32 {
    let area = a * a;
    area
}

const AREA: i32 = square_area(5);

The value 5 is known at compile time, so we know what square_area(5) is at compile time. It also ensures that what can be known at compile time, is actually known at compile. It becomes kind of a compile-time check which may result in an error in case we can’t compute the value at compile time.

Before release 1.33.0, you were not allowed to use let. You would have got below error message.

   Compiling variables v0.1.0 (/home/ayush/workspace/variables)
error: local variables in const fn are unstable
  --> src/main.rs:20:9
   |
20 |     let area = a * a;
   |         ^^^^

error: aborting due to previous error

error: Could not compile `variables`.

In above example, you will notice one more thing, I have called constant function square_area in const contexts. When called from a const context, the function is interpreted by the compiler at compile time. const fn will be guaranteed to evaluate at compile time when the result is stored into a constant. If we call it outside constant context, there will be no difference between const fn and a normal fn

A const function cannot call a non-const function. But reverse is true. You are also not allowed to define a const fn that can’t be evaluated at compile-time. For example- you cannot write

const fn x(a: i32) -> i32 {
    add(a)
}

fn add(x: i32) -> i32 {
    x + 1
}

Here you will get a compilation error. Rust’s const fn is still in unstable state. You can not define if, match, && and ||. I hope this will be improved in future releases. Thanks for reading this blog.


Knoldus-blog-footer-image

Written by 

Ayush is the Sr. Lead Consultant @ Knoldus Software LLP. In his 10 years of experience he has become a developer with proven experience in architecting and developing web applications. Ayush has a Masters in Computer Application from U.P. Technical University, Ayush is a strong-willed and self-motivated professional who takes deep care in adhering to quality norms within projects. He is capable of managing challenging projects with remarkable deadline sensitivity without compromising code quality.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading