Can't resolve core :: slice :: Iter as core :: iter :: Iterator?

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?

+4
source share
2 answers

There are a couple of things that confuse this error message.

You're dead, this is a pretty difficult message to parse.

, Rust core::slice::Iter core::iter::Iterator

: , . ( , !). , , :

type mismatch resolving `<core::slice::Iter<'_, T> as core::iter::Iterator>::Item == T`
                         (________________________________________________)

core::slice::Iter<'_, T> core::iter::Iterator, , <core::slice::Iter<'_, T> as core::iter::Iterator>::Item - . : , as core::slice::Iter<'_, T> core::iter::Iterator, Item.

IntoIterator :

pub trait IntoIterator where
    <Self::IntoIter as Iterator>::Item == Self::Item

. , . Item T IntoIter core::slice::Iter<'_, T>, .

, IntoIterator Item, , Item. core::slice::Iter<'a, T> Item :

type Item = &'a T

impl.

main(), .

+5

-, Rust core:: slice:: Iter core:: iter:: Iterator. , core:: slice:: Iter - , ? ?

:

<std::slice::Iter<'_, T> as std::iter::Iterator>::Item == T

* Item T.

, slice::Iter , .

+2

Source: https://habr.com/ru/post/1693929/


All Articles