【问题标题】:Is it possible to declare a static array of specified type without repeating the length?是否可以在不重复长度的情况下声明指定类型的静态数组?
【发布时间】:2020-05-16 18:33:15
【问题描述】:

我的理解是这样的

let x: [i16; 256] = [0; 256];

是声明i16 的静态数组并用零初始化它的惯用方式。有什么办法可以重复长度吗?例如,为什么不编译:

let x: [i16; 256] = [0; _];

【问题讨论】:

  • let x = [0i16; 256];?
  • @KentaroOkuda 谢谢,这就是我想要的

标签: arrays rust static initialization


【解决方案1】:

Rust 执行类型推断,因此您通常可以省略变量的类型。

fn main() {
    let x = [0i16; 256];
}

playground

【讨论】:

  • 谢谢,对我来说缺少的成分是 0i160
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-07
  • 2015-09-15
  • 2013-07-18
  • 2022-11-20
  • 1970-01-01
相关资源
最近更新 更多