Skip to content
Playground

std::text::str

This page covers immutable strings and their Index, Len, Add (concatenation), Display, and Debug operations.

Literals and concatenation

yulang
my name = "Yulang"

"hello"
"hello, " + name
"" + 1.show + " items"

+ on strings concatenates. The Add impl is the same role-based mechanism as + on numbers; user code can call s.add t instead.

Length

yulang
"hello".len    // 5

Len is exposed via .len.

Indexing and slicing

yulang
my s = "yulang"

s[0]            // char for y
s[s.len - 1]    // char for g
s[1..<3]        // "ul"
s[..2]          // "yul"
s[2..]          // "lang"

Strings implement Index for both int (a char) and range (a str substring).

Splicing

yulang
"abcd".splice (range 1 3) "XY"     // "aXYd"

splice returns a new string with the slice replaced. The original is unchanged.

Display and conversion

yulang
1.show              // "1"
true.show           // "true"
(1.5).show          // "1.5"
[1, 2, 3].show      // "[1, 2, 3]"
["a", "b"].show     // "[a, b]"

.show resolves through the Display role. The prelude ships user-facing Display impls for primitives, unit, list, opt, result, and common tuple arities when their payloads also implement Display; user types gain one through error E: (auto-generated) or by writing impl Display T: our v.show = .... Values that implement Display also get .say, which prints .show with a newline.

For structural developer-facing output, use .debug:

yulang
[1, 2, 3].debug      // "[1, 2, 3]"
(just "x").debug     // "just \"x\""

.debug resolves through the Debug role. The prelude provides impls for primitives, list, opt, result, and common tuple arities when their payloads also implement Debug. The basic runtime host also renders structural fallbacks for records and longer tuples, which is what yulang run and the playground use for inspection. Strings themselves have .print and .println, which write the raw string without or with a newline; these are str methods, not Debug methods.

Quick reference

OperationSignature
s.lenstr -> int
s + t / s.add tstr -> str -> str
s[i]str -> int -> char
s[r]str -> range -> str
s.splice r tstr -> range -> str -> str
value.showDisplay 'a => 'a -> str
value.debugDebug 'a => 'a -> str
value.sayDisplay 'a => 'a -> [out] ()
s.printstr -> [out] ()
s.printlnstr -> [out] ()

See also

  • std::data::list — list operations are often paired with strings
  • Strings — string-specific syntax and patterns
  • Casts — between strings and wrapper types

Yulang