[_;中的数组长度N可以使用什么表达式? N]? [英] What expressions are allowed as the array length N in [_; N]?

查看:194
本文介绍了[_;中的数组长度N可以使用什么表达式? 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,尽管后者受特性限制.仅在耗费大量时间后,我才发现实际上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

通用关联的const当前无法用于参数化固定数组长度(#42863 )

这不是特别好的错误消息

this is not a particularly good error message

对于数组长度相关的const,错误消息应该得到改善(#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可以使用什么表达式? N]?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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