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

TryCollect

Materializes a cursor into a new Vector with an explicitly owned allocator.

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.

TryCollect

pub fn TryCollect<T, I, A>(mut Cursor: I, mut AllocatorValue: A)
    -> Result<Vector<T, A>, AllocationError>
    where T: Movable where I: CollectibleIterator<T>, Movable where A: Allocator, Defaultable, Movable

Materializes a cursor into a new Vector with an explicitly owned allocator.

Allocates only through AllocatorValue and consumes the cursor in source order. Exact owning values transfer; borrowed elements copy into T through ordinary conversion, preserving their original backing. Failure destroys the partial Vector and rejected item before returning its stable allocation reason. Success publishes one owned collection. No pre-count or intermediate buffer is introduced. Work includes traversal, element lifecycles and Vector growth. T: Explicit destination element type; source items must convert normally. I: Movable structural cursor type, inferred from Cursor. A: Owned structural allocator, inferred from AllocatorValue. Cursor: Owned cursor state; move non-Copy state explicitly. AllocatorValue: Configured allocator transferred into the resulting collection. Return: Owned Vector on success; allocation reason after complete partial-state cleanup on failure. Warning: May allocate; source backing must remain live throughout traversal. No synchronization is added.

Example

module example.collect_cursor;
from std.iteration import TryCollect, Take, Sort;
from std.collections import Vector;
from std.memory import DefaultAllocator;
fn Compare(Left: readonly i32&, Right: readonly i32&) -> i32 {
    if Left < Right { return -1; } if Left > Right { return 1; } return 0;
}
fn Main() -> i32 {
    let Source: i32[3] = [22, 20, 99];
    var Prefix = Take<readonly i32&>(Source.CreateIterator(), 2);
    // Retain writable result storage while sorting its owned collection.
    var Collection = TryCollect<i32>(move(Prefix), default(DefaultAllocator));
    switch Collection {
        case .Ok { Value = Values: mut Vector<i32, DefaultAllocator>& } => {
            Sort<i32>(Values.AsSlice(), Compare);
            if Source[0] != 22 || Source[1] != 20 { return 1; }
            return Values.At(0) + Values.At(1) - 42;
        }
        case .Err { Error = Reason } => {
            if Reason == .SizeOverflow { return 2; }
            return 3;
        }
    }
}

TryCollect

pub fn TryCollect<T, I>(mut Cursor: I) -> Result<Vector<T>, AllocationError>
    where T: Movable where I: CollectibleIterator<T>, Movable

Materializes a cursor with the target-selected default allocator. T: Explicit destination value type, accepting ordinary item conversion. I: Movable structural Iterator cursor, inferred from Cursor. Cursor: Owned traversal state, consumed through first none or failure. Return: Owned Vector, or allocation reason after partial collection cleanup. Warning: May allocate; borrowed input backing must remain valid through this synchronous call.

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