首页 > 解决方案 > Unresolved import `core::array::FixedSizeArray`

问题描述

I am new to Rust. I've been given some Rust code to work with but the cargo build fails with error "unresolved import core::array::FixedSizeArray". In what way can I remove the error?

I'm using version 1.57.0 nightly; do not know which version of Rust the code base has been build successfully last time.

The code also uses a lot of '#![feature(...)]', which could not be used with stable version. How do I map a feature in older version's nightly build to the current version's functions/etc.? Example of features: #![feature(vec_resize_default)] #![feature(fixed_size_array)]

Thanks.

标签: rust

解决方案


最后一个版本FixedSizeArray是 1.52.1 版本。它被设计用于在不合适[T; N]的情况下&[T]一般使用固定大小的数组。AsRef<[T]>但即使在跟踪问题中,它的用处也是有限的,最终会被min_const_generics1.51.0 版本中稳定的功能所掩盖。

您将不得不将夜间版本降级回 1.52.1 或更早版本,或者修复由其删除引起的问题。没有办法只在较新版本上使用已删除的功能。

替换此功能的一些选项:

  1. [T; N]可以使用 const 泛型直接将其转换为形式。

    struct MyStruct<A: FixedSizeArray> {
        data: A
    }
    
    fn make_my_struct<A: FixedSizeArray>(data: A) -> MyStruct<A> {
        MyStruct { data }
    }
    

    会成为:

    struct MyStruct<T, const N: usize> {
        data: [T; N]
    }
    
    fn make_my_struct<T, const N: usize>(data: [T; N]) -> MyStruct<T, N> {
        MyStruct { data }
    }
    
  2. 也许您一开始并不需要它,只需使用&[T]or就可以了AsRef<[T]>

  3. 使用与板条箱具有相似用途Arrayarray_ext板条箱中的特征。

  4. 做你自己的特质。FixedSizeArray不需要任何编译器魔法,但要为所有数组类型实现它,无论如何你都必须使用上面的方法#1。


推荐阅读