See this code:
fn main() {
let something_const = 42;
fn multiply(nbr: i32) -> i32 {
nbr * something_const
}
println!("{}", multiply(1));
}
rustc
outputs that
error[E0434]: can't capture dynamic environment in a fn item; use the || { ... } closure form instead
--> main.rs:19:15
|
19 | nbr * something_const
| ^^^^^^^^^^^^^^^
But something_const
is not dynamic, because it is known at compile time.
Is it an equivalent in Rust of the C++ constexpr
mechanism?
The keyword constexpr was introduced in C++11 and improved in C++14. It means constant expression. Like const , it can be applied to variables: A compiler error is raised when any code attempts to modify the value. Unlike const , constexpr can also be applied to functions and class constructors.
#define (also called a 'macro') is simply a text substitution that happens during preprocessor phase, before the actual compiler. And it is obviously not typed. constexpr on the other hand, happens during actual parsing. And it is indeed typed.
Static specifies the lifetime of the variable. A static constexpr variable has to be set at compilation, because its lifetime is the the whole program. Without the static keyword, the compiler isn't bound to set the value at compilation, and could decide to set it later.
To be constexpr, a function must be rather simple: just a return-statement computing a value. A constexpr function can be used for non-constant arguments, but when that is done the result is not a constant expression.
constexpr
in C++ can be used in 2 different situations:
Rust supports both, albeit in a limited fashion:
const
to declare a constant, instead of let
, to declare that it is truly constantconst
to qualify a function, to declare that it can be evaluated at compile-time. Not all functions can be made const
yet.In your situation, you want the first usage:
fn main() {
const something_const: i32 = 42;
fn multiply(nbr: i32) -> i32 {
nbr * something_const
}
println!("{}", multiply(1));
}
Note that unlike with let
, it is mandatory to annotate the constant with its type.
Also, the compiler will complain about the naming; constants use ALL_CAPS
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With