[_; 中的数组长度 N 允许哪些表达式?否]? [英] What expressions are allowed as the array length N in [_; N]?

查看:20
本文介绍了[_; 中的数组长度 N 允许哪些表达式?否]?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下 Rust 中的最小示例:

Please consider the following minimal example in Rust:

const FOOBAR: usize = 3;

trait Foo {
    const BAR: usize;
}

struct Fubar();

impl Foo for Fubar {
    const BAR: usize = 3;
}

struct Baz<T>(T);

trait Qux {
    fn print_bar();
}

impl<T: Foo> Qux for Baz<T> {
    fn print_bar() {
        println!("bar: {}", T::BAR);   // works
        println!("{:?}", [T::BAR; 3]); // works
        println!("{:?}", [1; FOOBAR]); // works
        println!("{:?}", [1; T::BAR]); // this gives an error
    }
}

fn main() {
    Baz::<Fubar>::print_bar();
}

编译器给出以下错误:

error[E0599]: no associated item named `BAR` found for type `T` in the current scope
  --> src/main.rs:24:30
   |
24 |         println!("{:?}", [1; T::BAR]); // this gives an error
   |                              ^^^^^^ associated item not found in `T`
   |
   = help: items from traits can only be used if the trait is implemented and in scope
   = note: the following trait defines an item `BAR`, perhaps you need to implement it:
           candidate #1: `Foo`

无论我的问题的答案是什么,这都不是一个特别好的错误消息,因为它表明 T 确实实现了 Foo,尽管后者是一个 trait bound.只是在燃烧了很多时间之后,我才发现实际上 T::BAR 在其他上下文中是一个完全有效的表达式,只是不能作为数组的长度参数.

Whatever the answer to my question, this is not a particularly good error message because it suggests that T does implement Foo despite the latter being a trait bound. Only after burning a lot of time did it occur to me that in fact T::BAR is a perfectly valid expression in other contexts, just not as a length parameter to an array.

控制什么样的表达式可以去那里的规则是什么?因为数组是 Sized,我完全理解长度是在编译时知道的.我自己来自 C++,我希望有一些类似于 constexpr 的限制,但我在 文档 它只是说

What are the rules that govern what kind of expressions can go there? Because arrays are Sized, I completely understand that the length are to be known at compile time. Coming from C++ myself, I would expect some restriction akin to constexpr but I have not come across that in the documentation where it just says

一个固定大小的数组,表示为[T;N],对于元素类型,T,以及非负的编译时常量大小,N.

A fixed-size array, denoted [T; N], for the element type, T, and the non-negative compile-time constant size, N.

推荐答案

从 Rust 1.24.1 开始,数组长度基本上需要是数字文字或常规"usize 的常量.目前存在少量的持续评估,但或多或​​少仅限于基础数学.

As of Rust 1.24.1, the array length basically needs to either be a numeric literal or a "regular" constant that is a usize. There's a small amount of constant evaluation that exists today, but it's more-or-less limited to basic math.

在其他上下文中完全有效的表达式,只是不能作为数组的长度参数

a perfectly valid expression in other contexts, just not as a length parameter to an array

数组长度不支持通用参数.(#43408)

这不是一个特别好的错误信息

this is not a particularly good error message

应该改进数组长度中关联常量的错误消息 (#44168)

我希望有一些类似于 constexpr

本质上的限制,问题是允许在const中使用的内容目前受到高度限制.值得注意的是,这些是不允许的:

This is essentially the restriction, the problem is that what is allowed to be used in a const is highly restricted at the moment. Notably, these aren't allowed:

  • 函数(除了构造枚举或结构)
  • 循环
  • 多个语句/块

关于良好的常量/编译时评估的工作仍在进行中.有大量 RFC、问题和 PR 对此进行了改进.示例:

Work on good constant / compile-time evaluation is still ongoing. There are a large amount of RFCs, issues, and PRs improving this. A sample:

  • Const fn tracking issue (RFC 911)
  • Allow locals and destructuring in const fn (RFC 2341)
  • Allow if and match in constants (RFC 2342)

这篇关于[_; 中的数组长度 N 允许哪些表达式?否]?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆