swap

public func swap[T](mutating T, mutating T) where T: not Copyable, T: not Static

Exchanges the contents of two mutable locations without cloning or dropping either — three bitwise moves, like Rust's mem::swap. For a COW value (Array, String, Dictionary) this swaps the handles, so it never touches the heap buffers: an O(1) pointer exchange regardless of contents.

This is the idiomatic way to rotate double buffers (swap(a, b) instead of the let tmp = a; a = b; b = tmp dance, which clones for non-Copyable types).

Examples

var a = [1, 2, 3]; var b = [9, 8]; swap(a, b); // a == [9, 8], b == [1, 2, 3] — no element copies

Defined in lang/std/memory/pointer.ks