IntoIterator
trait IntoIterator<T>
Source: compiler/src/builtin_docs/prelude.hy A type that can be turned into an Iterator, which makes it usable in for x in value.
into_iter returns the iterator (IntoIter), whose Item must match.
A class that is its own iterator returns itself. See Iterator for a full
example.
Associated types#
type Itemtype IntoIter
Required methods#
| Name | Description |
|---|---|
into_iter | Converts value into its iterator. |
Examples#
use tests::assert;
class Ones {
pub left: int,
}
impl Iterator for Ones {
type Item = int;
pub fn next(Ones o) -> Option<int> {
if o.left == 0 {
return Option::None;
}
o.left = o.left - 1;
return Option::Some(1);
}
}
impl IntoIterator for Ones {
type Item = int;
type IntoIter = Ones;
pub fn into_iter(Ones o) -> Ones {
return o;
}
}
fn main() {
let n = 0;
for x in new Ones(4) {
n = n + x;
}
assert!(n == 4);
}