Сервера раст - Добро пожаловать!
Подключайтесь к лучшим серверам Rust, играйте в раст прямо сейчас! Просмотрите статистику и текущих игроков, выберите свой путь в мире выживания на серверах Rust.
Обсуждение на тему: Rust iterator
Rust iterator
For more about the concept of iterators generally, please see the module-level documentation. Iterators are heavily used in idiomatic rust code, so its worth becoming familiar with them. Before explaining more, lets talk about how this module is structured organization. This module is largely organized by type traits are the core portion these traits define what kind of iterators exist and what you can do with them. The methods of these traits are worth putting some extra study time into. Ниже представлен перевод одной из частей серии статей rust crash course от майкла сноймана, которая посвящена итераторам.
Ниже представлен перевод одной из частей серии статей rust crash course от майкла сноймана, которая посвящена итераторам. Мне материал показался удачным в плане доступности изложения, поэтому перевод, сделанный для себя, решил опубликовать. Iterators are a powerful tool that allow for efficient iteration over data structures, and they are implemented in many programming languages - including rust. However, rusts unique ownership system gives rise to interesting differences in how iterators are implemented and used. In this article, we will explore these differences and delve into rusts features by creating and implementing the most commonly used iterator traits - iterator and intoiterator.
Imagine you have a structure like this в rust итераторы ленивые (lazy), то есть они не делают ничего, пока вы не вызовете специальные методы, потребляющие итератор, чтобы задействовать его. Например, код в листинге 13-10 создаёт итератор элементов вектора v1, вызывая метод iter, определённый у vect. An iterators collect method can build any kind of collection from the rusts standard library, as long as the iterator produces a suitable item type. When some collection type like vec or hashmap knows how to construct itself from an iterator, it implements the stditerfromiterator trait, for which collect is a method obviously, rust comes with support for loops and iterators as well, and, just like in many other languages, iterators can be implemented from scratch. In this article were going to take a closer look at the iterator and intoiterator traits to create iterators and turning existing types into iterators as well.
Alright, lets start off by inspecting the following code snippet let names vec!pascal, elvira, dominic, christoph for name in names println!(, name) in rust, all iterators implement a trait named iterator that has a method called next(). Below is the code from the official rust documentation on how the iterator trait is defined. Pub trait iterator type item fn next(&mut self) - optionselfitem methods with default implementations elided. Dont worry about the new syntax all you need to understand is that when you attach the iter() method to make a list of values iterable, it gets the iterator trait implemented along with methods like next(). .