Fowlron
Fowlron

Reputation: 99

In which order are constant functions evaluated when assigned to constant variables?

When there are multiple constant variables in a rust program, and they get initialized (on compile time) by a constant function, in which order are they initialized? How can I assure safety if the initialization of one depends on the other? For example:

const FIRST : i32 = first_fun();
const SECOND : i32 = second_fun();

const fn first_fun() -> i32 {
    10
}

const fn second_fun() -> i32 {
    FIRST * 2
}

How can I guarantee correct initialization? Of course second_fun() could call first_fun(), but if first_fun() is computationally heavy that seems like unnecessary overhead, especially if one of the constants is an array.

Upvotes: 1

Views: 217

Answers (0)

Related Questions