Skip to Content
Language in depth

Library powers

The standard library has no power a user library lacks: operators, Show, operation values, identities and generators are ordinary declarations.

A power is a mechanism that lets library code feel built in. The standard library has no power a user library lacks: if a std module needs something, it is a listed power or core syntax, and your own library can use it the same way. The normative list is Powers: what a user library can define.

Operators on your types

fn (a: T) + (b: T) -> R defines an operator the way it reads. It is the Add trait's impl, so generic code bounded by T: Add accepts your type, and += comes with it:

Output
Press Run (Ctrl+Enter).

(W9)

Printing and reading

A type prints through Show and reads through Read. Structural types derive both; a type that wants its own form writes the impl, and print and interpolation use it:

Output
Press Run (Ctrl+Enter).

Operation values and identities

Algebra is passed as values: SegTree(+), SparseTable(min), a.scan(gcd). The identity comes from the element type (+ uses zero, min over ?T uses None), and anything else is a Monoid you build from an identity and an operation, exactly as std's own structures do:

Output
Press Run (Ctrl+Enter).

(W10)

Generators and places

A library's loop-shaped facility is a generator (-> yields T), so its callers get break, return and every adapter for free:

Output
Press Run (Ctrl+Enter).

A type defines what x[k] is as a place with IndexMut (a real place, as Map does) or IndexSet (write-through, as SegTree does, since an update must recompute ancestors), so t[i] = v and t[i] += v read the same on your structure as on an array (W8).

Why it's designed this way

No privileged stdlib. If std needs a power and users don't get it, every personal library is second class, and contestants copy std code instead of extending it. Keeping the list closed and shared means a technique you write once has std's ergonomics and speed.

Operators spelled as they read. Rust's impl Add for T { type Output = T; fn add(self, ...) } is five lines of protocol for one line of meaning, and C++'s operator+ is a separate rule from every other method. decision 133 replaced hand-written impl Add for T with fn (a: T) + (b: T), one spelling that is still the trait impl underneath.

Operations are values, not trait impls. A segment tree of max and one of + over the same Int are both common; making the operation a value instead of an impl Monoid for Int keeps both one line. decision 127 keeps one function per name, so SegTree(+) names exactly one function.

Places are a protocol. The alternatives were t.set(i, v) methods, which make a Fenwick tree or a lazy segment tree read differently from an array, and C++'s proxy reference returned from operator[], which hides the work and can dangle. decision 128 defined the two place protocols so a structure that must do work on write can still be written with t[i] = v, and the borrow rules still apply.

On this page