使用常量表达式声明数组的大小 [英] Declaring array using a constant expression for its size

查看:234
本文介绍了使用常量表达式声明数组的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在数组周围有一个新型包装器.我以为我可以使用size_of而不是手动传递数组的大小,但是编译器认为我错了.

I have a newtype wrapper around an array. I assumed that I could use size_of instead of manually passing the size of the array around, but the compiler thinks I'm wrong.

use std::mem::{size_of, size_of_val};

#[repr(C, packed)]
struct BluetoothAddress([u8, ..6]);

fn main() {
    const SIZE: uint = size_of::<BluetoothAddress>();

    let bytes = [0u8, ..SIZE];
    println!("{} bytes", size_of_val(&bytes));
}

(游戏围栏链接)

我每天晚上使用:rustc 0.13.0-nightly(7e43f419c 2014-11-15 13:22:24 +0000)

I'm using the nightly: rustc 0.13.0-nightly (7e43f419c 2014-11-15 13:22:24 +0000)

此代码失败,并出现以下错误:

This code fails with the following error:

broken.rs:9:25: 9:29 error: expected constant integer for repeat count, found variable
broken.rs:9     let bytes = [0u8, ..SIZE];
                                    ^~~~
error: aborting due to previous error

对数组表达式的严格引用使我认为这应该工作:

The Rust Reference on Array Expressions makes me think that this should work:

[expr ',' ".." expr]形式中,".."之后的表达式必须是可以在编译时求值的常量表达式,例如文字或静态项目.

In the [expr ',' ".." expr] form, the expression after the ".." must be a constant expression that can be evaluated at compile time, such as a literal or a static item.

推荐答案

您的SIZE定义不合法;只是其中的错误发生在阵列构造上的错误之后.如果只是将[0u8, ..SIZE]更改为[0u8, ..6],以使该部分正常工作,则会发现SIZE声明存在的问题:

Your SIZE definition is not legal; it’s just that the errors in it occur later than the error on the array construction. If you change [0u8, ..SIZE] to [0u8, ..6] just so that that part works, you find the problems with the SIZE declaration:

<anon>:7:24: 7:53 error: function calls in constants are limited to struct and enum constructors [E0015]
<anon>:7     const SIZE: uint = size_of::<BluetoothAddress>();
                                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<anon>:7:24: 7:51 error: paths in constants may only refer to items without type parameters [E0013]
<anon>:7     const SIZE: uint = size_of::<BluetoothAddress>();
                                ^~~~~~~~~~~~~~~~~~~~~~~~~~~

您根本无法像现在这样拨打size_of.

You simply can’t call size_of like that at present.

另一种方法是反转事物,使SIZE是规范的定义,而其他地方使用它:

An alternative is to invert things so that SIZE is the canonical definition and the other places use it:

use std::mem::{size_of, size_of_val};

const SIZE: uint = 6;

#[repr(C, packed)]
struct BluetoothAddress([u8, ..SIZE]);

fn main() {
    let bytes = [0u8, ..SIZE];
    println!("{} bytes", size_of_val(&bytes));
}


更新:使用Rust 1.0,该问题已被有效淘汰,并且编译器错误消息已得到改进,以使它们更加清晰.


Update: With Rust 1.0, this question has been effectively obsoleted, and the compiler error messages have been improved so that they are even more clear.

此外,随着最近登陆的#42859 ,rustc每晚将允许使用size_of在恒定的上下文中,前提是该板条箱具有#![feature(const_fn)](并且#43017 着陆,也将不再需要它,然后它将过滤通过以保持稳定.)

Furthermore, with #42859 recently landed, rustc nightly will allow using size_of in a constant context, provided the crate has #![feature(const_fn)] (and when #43017 lands, that won’t be needed any more either, and then it will filter through to stable).

换句话说,对语言的改进不再使它成为问题.

In other words, improvements to the language have made this no longer an issue.

这篇关于使用常量表达式声明数组的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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