我有个关于生锈里的仿制药的小问题。我想使Array的长度成为一个类型参数。
问题: Rust目前是否支持这个功能?错误消息并不表示缺少的特性,而是一个编程错误。
我想要做的事情的例子:
fn create_array<C: ConstSize>(){
    let arr = [64; C::SIZE];
    println!("array.len: {:?}", arr.len());
}
pub trait ConstSize {
    const SIZE: usize;
}通过一些示例实现:
fn main() {
    create_array::<Five>();
}
struct Five {}
impl ConstSize for Five {
    const SIZE: usize = 5;
}但是编译器告诉我:no associated item named 'SIZE' found for type 'C' in the current scope
然而,以下工作:
fn create_array(){
    let arr = [64; Five::SIZE];
    println!("array.len: {:?}", arr.len());
}参见锈菌游乐场上的示例
感谢您的关注。
发布于 2018-07-14 12:28:50
由于编译器错误(第52070期),关联常量不能用于指定数组的长度。
https://stackoverflow.com/questions/51338002
复制相似问题