Vector of elements in a iterator

  Kiến thức lập trình

First of all, I haven’t much experience with rust, so I’m probably misunderstanding some points. I am trying to make a object(struct) that iterate over the combinations of the elements in a iterator. I need to store a reference of the iterator’s elements in the struct but i was not able to figure out how to do it. This is my code:


pub struct Combination<'a, T: Iterator> {
    _obj_items_: Vec<&'a T::Item>,
    _com_: Vec<usize>,
}

impl<'a, T: Iterator> Combination<'a, T> {
    pub fn new (_obj_: &'a mut T, many: usize) -> Combination<'a, T> {
        let mut r = Combination::<'a, T> {
            _obj_items_ : Vec::<&'a T::Item>::with_capacity(_obj_.by_ref().count()),
            _com_ : Vec::<usize>::with_capacity(many),
        };

        r._com_.append(&mut (0..many).collect());

        /* filling vector with _obj_ elements. */
        for e in _obj_.by_ref() {
            r._obj_items_.push(&e);
        }

        r
    }
}

what I thought is that when I make _obj_.by_ref() (in the loop below the comment) it will iterate over the elements of the iterator, but I think that it is iterating over the elements of the iterator that contain the elements. Am I right? How can I make my code properly store a reference of the actual elements?

LEAVE A COMMENT