因此,我try 为没有堆分配的no_std环境实现一个简单的队列,如下所示:
pub struct Queue<T: Sized, const SIZE: usize> {
pub data: [Option<T>; SIZE],
}
impl<T: Sized, const SIZE: usize> Queue<T, SIZE> {
pub const fn new() -> Queue<T, SIZE> {
const DEFAULT: Option<T> = None;
Queue {
data: [DEFAULT; SIZE],
}
}
}
在我看来,这应该是可能的,因为T是有大小的,None
值可以是const
,所以它可以被复制,而不需要T实现Copy
特征.
然而,我似乎想不出该怎么做.
上面的语法给出了一个错误,其中包含一个非常无用的提示,建议我将T
替换为T
:
error[E0401]: can't use generic parameters from outer function
--> src/queue.rs:11:31
|
9 | impl<T: Sized, const SIZE: usize> Queue<T, SIZE> {
| - type parameter from outer function
10 | pub const fn new() -> Queue<T, SIZE> {
11 | const DEFAULT: Option<T> = Default::default();
| - ^ use of generic parameter from outer function
| |
| help: try using a local generic parameter instead: `<T>`
有什么办法可以做到这一点吗?顺便说一下,我用的是2021年版的铁 rust .