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

VectorConsumingIterator

Owns a transferred vector and yields its elements in sequence order. Construction allocates nothing and transfers backing/allocator ownership. Next moves exactly one indexed referent, preserving each valid moved-from slot for normal vector cleanup. It never shifts elements or allocates a second buffer. Traversal bookkeeping is O(1); T lifecycle costs remain explicit. Destruction releases remaining values, moved-from slots and backing normally. No element references escape. Serialize traversal and cursor movement.

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.collections

import std.collections;

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

VectorConsumingIterator

pub final class VectorConsumingIterator<T, A>
    where T: Movable where A: Allocator, Defaultable, Movable

Owns a transferred vector and yields its elements in sequence order. Construction allocates nothing and transfers backing/allocator ownership. Next moves exactly one indexed referent, preserving each valid moved-from slot for normal vector cleanup. It never shifts elements or allocates a second buffer. Traversal bookkeeping is O(1); T lifecycle costs remain explicit. Destruction releases remaining values, moved-from slots and backing normally. No element references escape. Serialize traversal and cursor movement. T: Movable payload yielded by ownership transfer. A: Movable Defaultable allocator owner satisfying Allocator.

Example

module example.vector_consuming;
from std.collections import Vector, CreateConsumingIterator;
from std.memory import Unique;
fn Main() -> i32 {
    var Values = Vector<Unique<i32>>();
    var First = {
        // Keep writable result storage through the explicit noncopy payload transfer.
        var FirstCreation = Unique<i32>.TryCreate(20);
        switch FirstCreation {
            case .Ok { Value = Value: mut Unique<i32>& } => break move(Value);
            case .Err => return 1;
        }
    };
    var Second = {
        // Keep writable result storage through the explicit noncopy payload transfer.
        var SecondCreation = Unique<i32>.TryCreate(22);
        switch SecondCreation {
            case .Ok { Value = Value: mut Unique<i32>& } => break move(Value);
            case .Err => return 2;
        }
    };
    if Values.TryPush(move(First)) is .Err { Error = _ } { return 3; }
    if Values.TryPush(move(Second)) is .Err { Error = _ } { return 4; }
    var Cursor = CreateConsumingIterator(move(Values));
    if !Values.IsEmpty { return 5; }
    var One = {
        // Keep writable result storage through the explicit noncopy payload transfer.
        var OneCreation = Cursor.Next();
        switch OneCreation {
            case .Some { Value = Value: mut Unique<i32>& } => break move(Value);
            case .None => return 6;
        }
    };
    if One.Value().Unwrap() != 20 { return 7; }
    var Moved = move(Cursor);
    if Cursor.Next() is .Some { Value = _ } { return 8; }
    var Two = {
        // Keep writable result storage through the explicit noncopy payload transfer.
        var TwoCreation = Moved.Next();
        switch TwoCreation {
            case .Some { Value = Value: mut Unique<i32>& } => break move(Value);
            case .None => return 9;
        }
    };
    if Two.Value().Unwrap() != 22 { return 10; }
    if Moved.Next() is .Some { Value = _ } { return 11; }
    if Moved.Next() is .Some { Value = _ } { return 12; }
    return 0;
}

VectorConsumingIterator.init

pub init(Source: readonly Self&) = delete;

Rejects duplicate payload/backing ownership. Source: Existing cursor.

VectorConsumingIterator.init

pub init(Source: mut Self&&)

Transfers backing and the next original index without a pull. Source: Cursor left with empty backing.

VectorConsumingIterator.operator

pub fn operator =(Source: readonly Self&) -> void = delete;

Rejects duplicate traversal/backing ownership. Source: Existing cursor. Return: No runtime result; this assignment is deleted and cannot be invoked.

VectorConsumingIterator.operator

pub fn operator =(Source: mut Self&&) -> void = delete;

Rejects implicit replacement of partially consumed backing. Source: Existing cursor. Return: No runtime result; this assignment is deleted and cannot be invoked.

VectorConsumingIterator.Next

pub fn Next() -> T?

Transfers the next original element, or reports sticky exhaustion. Return: Owned T, or none after all original slots have been yielded. Warning: Selected move operations must leave their source destructible.

CreateConsumingIterator

pub fn CreateConsumingIterator<T, A>(mut Source: Vector<T, A>) -> VectorConsumingIterator<T, A>
    where T: Movable where A: Allocator, Defaultable, Movable

Creates an owned vector cursor without new allocation or element movement. T: Movable payload inferred from Source. A: Movable Defaultable allocator inferred from Source. Source: Owned vector; move a named owner explicitly. Return: Concrete noncopy cursor yielding owned T in source order. Warning: Transfers backing; original views follow ordinary move invalidation.

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