【问题标题】:Is there a way to initialize an empty slice?有没有办法初始化一个空切片?
【发布时间】:2018-01-13 23:31:13
【问题描述】:

这样的?

[String, 0]

Vec::new() 不是一个选项。

【问题讨论】:

    标签: arrays rust slice


    【解决方案1】:

    这会创建一个空数组:

    let thing: [String; 0] = [];
    

    你也可以从数组中获取切片:

    let thing: &[String] = &[];
    

    你也可以使用as:

    some_function([] as [String; 0]);
    some_function(&[] as &[String]);
    

    【讨论】:

      猜你喜欢
      • 2017-01-22
      • 1970-01-01
      • 2015-02-20
      • 2014-05-17
      • 2015-05-23
      • 2021-06-09
      • 2021-02-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多