如何使用在编译时已知大小的泛型Sized
作为基于其大小的结构数组长度
我有以下错误:
error: generic parameters may not be used in const operations
--> src/lib.rs:4:43
|
4 | arr: [Key; 1024 / std::mem::size_of::<Key>()]
| ^^^ cannot perform const operation using `Key`
|
= note: type parameters may not be used in const expressions
对于以下代码
type SomeKey = u64;
// This does not work
struct NotWorking<Key: Sized> {
arr: [Key; 1024 / std::mem::size_of::<Key>()]
}
struct Working {
arr: [SomeKey; 1024 / std::mem::size_of::<SomeKey>()]
}