Why can't I use a function that returns a compile-time constant as a constant?

let arr0 = [0u8; 15]; let arr1 = [0u8; arr0.len()]; // this fails 

I think that the compiler should be able to determine the length of arr0 as a compile-time constant, no? However, this is flagged as an error indicating that the variable was found instead of a constant integer.

  • Why?
  • Is there a constexpr (C ++) function in Rust?

Version:

 rustc 1.0.0-nightly (ecf8c64e1 2015-03-21) (built 2015-03-22) 
+6
source share
1 answer

Because it has not yet been implemented. The extension of the Rust subset, which is considered a constant expression, can be made backward compatible, so you don’t have to rush to 1.0 and he didn’t even decide how it should be done (how much should be allowed, whether constexpr should be and how efficient it is , etc.).

Meanwhile, macros and syntax extensions cover many of the same use cases (and the latter are more powerful than constexpr will ever be).

+15
source

Source: https://habr.com/ru/post/984227/


All Articles