What's the difference between "Higher-Kinded rank" and "Higher-Kinded type"


#1

I’m trying to understand the for<'a> syntax and why it doesn’t solve the HKT problem in, for example

pub struct Container<'a> {
    slice: &'a [u8],
    pos: usize
}

impl<'a> Iterator for Container<'a> {
    type Item = &'??? [u8];

    fn next(&mut self) -> Option<Self::Item> {
        let slice = self.slice[pos..pos+10];
        pos += 1;
        Some(slice)
    }
}

why can’t you just do

pub struct Container<'a> {
    slice: &'a [u8],
    pos: usize
}

impl<'a, 'b: 'a> Iterator for Container<'a> {
    type Item = &'b [u8];

    fn next(&mut self) -> Option<Self::Item> {
        // ignoring array bounds for simplicity
        let slice = self.slice[pos..pos+10];
        pos += 1;
        Some(slice)
    }
}

#2

This case is much simpler - you can simply do:

pub struct Container<'a> {
    slice: &'a [u8],
    pos: usize
}

impl<'a> Iterator for Container<'a> {
    type Item = &'a [u8];

    fn next(&mut self) -> Option<Self::Item> {
        let slice = &self.slice[self.pos..self.pos+10];
        self.pos += 1;
        Some(slice)
    }
}

That’s because the Container already has a (immutable) reference to slice given to it from outside so it can happily return slices to it.

The associated type constructors (which is probably what you’re asking about) would, however, allow you to have an associated item that has its own lifetime parameter. http://smallcultfollowing.com/babysteps/blog/2016/11/02/associated-type-constructors-part-1-basic-concepts-and-introduction/ has some examples.