Is it possible to control the size of an array usi

2019-01-04 03:07发布

What follows is just used as an example, and not valid Rust code.

struct Vec<T: Sized, Count> {
    a: [T; Count]
}

Something like it is possible in C++ templates, but I haven't seen it in Rust.

标签: rust
2条回答
干净又极端
2楼-- · 2019-01-04 03:41

While waiting for Rust to gain first-class support for this, there are crates that provide certain levels of this functionality, such as:

查看更多
地球回转人心会变
3楼-- · 2019-01-04 03:54

If you look at the design of Rust, you will notice that it started first by tackling the hardest problems (memory-safe, data-race free) but there are otherwise lots of areas where it is "incomplete" (compared to what could be achieved).

In particular, generic structures and functions are somewhat limited today:

  • lack of Higher Kinded Types (HKT)
  • lack of non-type parameters => arrays are special-cased, and implementing a trait for an array is a known issue, the work-around being to implement it for a few different dimensions
  • lack of variadic parameters => tuples are special-cased, and implementing a trait for all tuples is similarly difficult

For the moment, those are not implemented, not because they are not desired but simply because time was lacking. The idea of Rust 1.0 was not to release a final product that would not evolve, but a stable base from which to start; some or maybe all will come.

As for the roadmap? Well, there is none that I know of though post-1.0 is certain, I invite you to keep your ear to the ground.

查看更多
登录 后发表回答