Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
995 views
in Technique[技术] by (71.8m points)

rust - Why can't I use a function returning a compile-time constant as a constant?

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

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

  1. Why?
  2. Is there constexpr (C++) function in Rust?

Version:

rustc 1.0.0-nightly (ecf8c64e1 2015-03-21) (built 2015-03-22)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Because it hasn't been implemented yet. Extending the subset of Rust that counts as constant expressions can be done backwards-compatibly, so there's no rush to do so before 1.0, and it's not even settled how it should be done (how much should be allowed, whether there should be a constexpr mechanism and how powerful it should be, etc).

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...