Wado

WEP: String Views — StrSlice and AsStrSlice

Context

Rust lets one signature accept String, &String, and &str through AsRef<str>. Wado answers two thirds of that already. Value semantics plus value-copy elision (WEP: Ownership Analysis) mean an owned String and a &String both reach a &String parameter without copying, and static trait dispatch monomorphizes a generic bound away at -O2.

The missing third is the view: a subrange of a string that is neither a copy nor a reference to the whole. Without one, an API that works on part of a string passes the part as arguments. The standard library did exactly that. FromStr::from_str_range(s, start, end) took the range beside the string, and every caller repeated the triple.

ByteSlice already views a String's bytes with no copy, but it views bytes, not text: nothing holds its ends on character boundaries. A newtype cannot hold one either. as converts in both directions for free, so type ByteSlice = Slice<u8> admits exactly what Slice<u8> admits (WEP: Newtype Semantics).

Zero overhead here means zero after -O2, not zero in the source. A view is a three-field aggregate built and consumed inside one function; the optimizer is what has to make it vanish.

Decision

A string view is a library type, not a compiler-known one. The mechanism must be one a user could build for their own type, so nothing in the compiler knows the name StrSlice.

A newtype is not the shape for it, since it cannot hold the UTF-8 boundary invariant. docs/cheatsheet.md and docs/spec-types.md say so where they introduce newtypes.

core:prelude gains StrSlice, a view over a string's bytes whose ends are character boundaries, and AsStrSlice, the conversion that lets one signature take a String, a reference to one, or a view of one. Where such a view still costs something at -O2, the optimizer is fixed rather than worked around.

A parameter that only reads its text names AsStrSlice and takes it by value, across the standard library and the packages alike. A call site passes the literal bare.

What follows:

Known gaps