Under heavy development. This site describes the intended 1.0 language. Current features and performance vary. Development status
Sn50SnThe SnAPI programming language
Development · Evolving reference

TakeIterator

Owns lazy cursor state that yields at most a requested number of items.

Sn is under heavy development. This reference includes the intended 1.0 design; features may be incomplete in the current compiler. Read the status →

Module std.iteration

import std.iteration;

Public declaration contracts and authored examples. A page can document several related symbols. Implementation bodies are excluded.

TakeIterator

pub final class TakeIterator<T, I> where I: TypedIterator<T>, Movable where T: Movable

Owns lazy cursor state that yields at most a requested number of items.

Construction performs no pulls and allocates nothing. Next calls its source at most once per delivered item, and never after its limit or first none. Reference payloads retain exact access and expire with the source's backing; value payloads transfer through their optional carrier. The adapter owns its source cursor, and destruction releases any ownership held by that cursor. It adds no synchronization; serialize traversal and honor source invalidation. T: Exact source item type, including reference access. I: Structural source cursor with Next returning T?.

Example

module example.take_iterator;
from std.iteration import Take;
fn Main() -> i32 {
    var Values: i32[3] = [20, 20, 99];
    var Prefix = Take<mut i32&>(Values.CreateWritableIterator(), 2);
    while Prefix.Next() is .Some { Value = Item } { Item += 1; }
    if Values[0] != 21 || Values[1] != 21 || Values[2] != 99 { return 1; }
    if Prefix.Next() is .Some { Value = _ } { return 2; }
    var Empty = Take<i32&>(Values.CreateIterator(), 0);
    if Empty.Next() is .Some { Value = _ } { return 3; }
    return 0;
}

TakeIterator.Next

pub fn Next() -> T?

Pulls one item if the limit and source permit it. Return: Exact source item or none; reference payloads retain their access. Warning: The source's backing lifetime and thread rules remain unchanged.

Take

pub fn Take<T, I>(mut Source: I, Limit: usize) -> TakeIterator<T, I>
    where I: TypedIterator<T>, Movable
    where T: Movable

Creates a lazy prefix cursor without pulling its input. T: Exact item type, supplied explicitly under v1 generic rules. I: Structurally checked cursor type, inferred from Source. Source: Traversal state; move a non-Copy cursor to transfer its ownership. Limit: Maximum items; zero never calls the source's Next. Return: Caller-owned adapter, with no allocation or extra backing retention. Warning: Borrowed source storage must survive traversal and every yielded alias.

Documentation snapshot: October 24, 2026 About versions ↗
Searching Development · language & standard library