What’s new in Rust 1.46.0 about const_fn?

Reading Time: 3 minutes

Hello, folks! your wait is over, we have come up with a new blog. In this blog, we will discuss the new update about const_fn in the Rust update 1.46.0. I hope you will enjoy the blog.

Improvement in Const Function

The const_fn feature allows marking free functions and inherent methods as const, enabling them to be called in constants contexts, with constant arguments.

Example:

const fn square(x: i32) -> i32 {
    x * 2
}

const DIGIT: i32 = 10;
const RESULT: i32 = square(DIGIT);

fn main() {
    println!("The result of const function: {}",RESULT);
}

Output:

The result of const function: 20

So the new Rust stable release 1.46.0 enables quite a lot of new things to appear in const_fn, two new standard library APIs.

1. Use of if, if let, and match:

Now we can use the if, if let, and match in the const function which help to use conditions in the const function.

Example: .

// Use of if statement in const function.
const fn even(number: i32) -> i32 {
    if number %2 == 0 {
        number
    } else {
        number + 1
    }
}

// Use of match statement in const function.
const fn even_no (number: i32) -> i32 {
    match number {
        2 => number,
        _ => number + 1 ,
    }
}

const DIGIT: i32 = 9;
const RESULT: i32 = even(DIGIT);
const RESULT_MATCH: i32 = even_no(DIGIT);

fn main() {
    println!("The result of const function with if statement: {}", RESULT);
    println!("The result of const function with match statement: {}", RESULT_MATCH);
}

Output:

The result of const function with if statement: 10
The result of const function with match statement: 10

2. Use of while, while let, and loop:

After this release we can also use the while, while let, and loop in the const function which help to use loops in the const function.

Example:


// Use of while loop in const function.
const fn difference(mut number: i32) -> i32 {
    let mut counter = 0;
    while number <= 12 {
        counter = counter + 1;
        number = number +1;
    }
    counter
}

// Use of loop in const function.
const fn loop_fn (number: i32) -> i32 {
    loop {
         if number == 9  
            {
            break;
       }
    }
    number
}

const DIGIT: i32 = 9;
const RESULT: i32 = difference(DIGIT);
const RESULT2: i32 = loop_fn(DIGIT);

fn main() {
    println!("The result of const function with while loop: {}", RESULT);
    println!("The result of const function with loop: {}", RESULT2);
}

Output:

The result of const function with while loop: 4
The result of const function with loop: 9

3. Use of && and || operators:

This release enables the && and || operators in the const function which help to use operators in the const function.

Example:

// Use of && and || operators in const function.
const fn operators(number: i32) -> i32 {
    if number >= 5 && number <=10 {
        if number == 8 || number == 9 {
            number
        } else {
        0
      }
    } else {
        0
      }
}

const DIGIT: i32 = 9;
const RESULT: i32 = operators(DIGIT);

fn main() {
    println!("The result of const function with operators: {}", RESULT);
}

Output:

The result of const function with operators: 9

Note: I hope our blogs help you to enhance your learning. I’ll post more blogs on Rust. Stay Tuned.

If you want to read more content like this?  Subscribe Rust Times Newsletter and receive insights and latest updates, bi-weekly, straight into your inbox. Subscribe Rust Times Newsletter: https://bit.ly/2Vdlld7 .

Happy learning!!!

This image has an empty alt attribute; its file name is screenshot-from-2020-06-08-11-00-35.png
This image has an empty alt attribute; its file name is footer-2.jpg

Written by 

Pankaj Chaudhary is a Software Consultant at Knoldus LLP. He has 1.5+ years of experience with good knowledge of Rust, Python, Java, and C. Now he is working as Rust developer and also works on machine learning and data analysis because he loves to play with data and extract some useful information from it. His hobbies are bike riding and explore new places.

Leave a Reply