I am trying to write a simple iterator in Rust:
#[derive(Debug)]
pub struct StackVec<'a, T: 'a> {
storage: &'a mut [T],
len: usize,
_head: usize,
}
impl<'a, T> IntoIterator for StackVec<'a, T> {
type Item = T;
type IntoIter = core::slice::Iter<'a, T>;
fn into_iter(self) -> core::slice::Iter<'a, T> {
self.storage.iter()
}
}
However, trying to compile it, I get this error:
error[E0271]: type mismatch resolving `<core::slice::Iter<'_, T> as core::iter::Iterator>::Item == T`
--> src/lib.rs:135:13
|
135 | impl<'a, T> IntoIterator for StackVec<'a, T> {
| ^^^^^^^^^^^^ expected reference, found type parameter
|
= note: expected type `&T`
found type `T`
error: aborting due to previous error
error: Could not compile `stack-vec`.
There are a couple of things that confuse this error message. First, it seems that Rust cannot resolve core::slice::Iterhow core::iter::Iterator. But, core::slice::Iteris an iterator, right? Why don't these types match?
Secondly, I see an error, expecting to IntoIteratorbe a reference, not a type parameter. However, this is not a type parameter. What does it mean?
What am I doing wrong here? What is Rust trying to tell me about my code?
source
share