Concrete steps toward RFC 3550 (new Range types)
NYKevin
·
2026-04-21
·
via LWN.net comments
To further explain this for people not steeped in Rust standard library traits: * IntoIterator should be understood as "a collection that we can iterate over, or a reference or view into such a collection." The term "collection" is meant somewhat liberally, for example an Option<T> is regarded as a collection which is either empty or contains exactly one T object. * Iterator should be understood as "a handle produced by an in-progress iteration operation, or an object which can be trivially used as such a handle." Every Iterator is also considered an IntoIterator. This can be justified either on the basis that every iterator is a view into a container, or on the far more prosaic basis that converting an iterator into an iterator is trivial. * For completeness, FromIterator should be understood as "a collection that we can build from a sequence of individual values." Implementing this trait enables Iterator::collect() to produce a given container type. Most collections implement both FromIterator and IntoIterator, but references or views only implement IntoIterator because FromIterator is designed to produce an owned collection. Also, some collections (like the aforementioned Option<T>) have invariants which cannot be enforced through collect()'s signature (Option can only hold one object), so they don't implement FromIterator at all. In C++ terms, FromIterator is an array, vector, map, etc., Iterator is the type returned by std::begin() (or the equivalent method), and IntoIterator is either of the above as well as view types (std::span), pointers, and whatnot. These equivalences are not exact because Rust does iteration differently to C++, but that is a story for another time.
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。