Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::iter::IntoIterator;
- struct MyStructIterator<'a, T> {
- items: std::iter::Skip<std::slice::Iter<'a, T>>,
- }
- impl<'a, T> MyStructIterator<'a, T> {
- fn new(elements: &'a Vec<T>) -> MyStructIterator<'a, T> {
- let items = elements.iter();
- MyStructIterator {
- items: items.skip(3),
- }
- }
- }
- impl<'a, T> Iterator for MyStructIterator<'a, T> {
- type Item = &'a T;
- fn next(&mut self) -> Option<Self::Item> {
- self.items.next()
- }
- }
- struct MyStruct<T> {
- elements: Vec<T>,
- }
- impl<'a, T> IntoIterator for &'a MyStruct<T> {
- type Item = &'a T;
- type IntoIter = MyStructIterator<'a, T>;
- fn into_iter(self) -> Self::IntoIter {
- MyStructIterator::new(&self.elements)
- }
- }
- fn main() {
- let my_struct = MyStruct {
- elements: vec![
- "a".to_string(),
- "b".to_string(),
- "c".to_string(),
- "d".to_string(),
- "e".to_string(),
- ],
- };
- let mut count = 0;
- for elem in &my_struct {
- println!("{}", elem);
- count += 1;
- }
- assert!(count == 2);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement