本文介绍了为什么我不能使用将编译时常量作为常量返回的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我认为编译器应该能够将 arr0 的长度确定为编译时常量,不是吗?这仍然被标记为错误,说明找到了变量而不是常量整数.

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. 为什么?
  2. Rust 中有 constexpr (C++) 函数吗?

版本:

rustc 1.0.0-nightly (ecf8c64e1 2015-03-21) (built 2015-03-22)

推荐答案

因为还没有实现.扩展 Rust 算作常量表达式的子集可以向后兼容,所以在 1.0 之前不急于这样做,甚至还没有确定应该如何完成(应该允许多少,是否应该有一个 constexpr 机制以及它应该有多强大等).

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).

与此同时,宏和语法扩展涵盖了许多相同的用例(后者比constexpr 更强大).

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).

这篇关于为什么我不能使用将编译时常量作为常量返回的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 06:38