Skip to content
Playground

Strings

Yulang's string type is str. Strings are UTF-8 text. Indexing and slicing use Unicode scalar-value positions exposed by the standard library, not raw byte offsets.

Literals

yulang
"hello"
"a\nb\u{21}"
"""
line1
line2
"""

String literals support common escapes such as \n and Unicode escapes such as \u{1F600}. Triple-quoted strings may span multiple lines.

Interpolation

yulang
my name = "yu"
"hello %{name}"
"n = %{12}"
"ok = %{true}"

Interpolation formats a value through the Display role. The standard prelude provides Display impls for primitives, list, opt, result, and common tuple arities when their payloads also implement Display.

Integer hex formatting is available through the lower- and upper-hex roles:

yulang
"hex = %x{255}"
"HEX = %X{255}"

Format specifications

A format specification appears between % and the interpolation body:

text
%[[fill]align][sign][#][0][width][.precision][kind]{expression}

The parts must appear in that order. The available markers are:

PartFormsEffect
align<, >, ^Left, right, or center alignment
sign+, -Show a plus sign, or keep only a negative sign
##Alternate form; hexadecimal values gain 0x or 0X
00Pad a numeric value with zeroes
widthDecimal digitsMinimum width of the result
precision. and decimal digitsMaximum length of the rendered body
kind?, x, XDebug, lower-hex, or upper-hex rendering

A character immediately before an alignment marker is the fill character. Precision clips the rendered body before width and alignment add padding:

yulang
my text = "abcdef"
my side = "right"
my quoted = "text"

(
    "%8.3{text}",       // "     abc"
    "%*>8{side}",       // "***right"
    "%#x{255}",         // "0xff"
    "%+#08x{255}",      // "+0x000ff"
    "%?{quoted}"        // "\"text\""
)

An omitted kind uses Display. The ? kind uses Debug; x and X use the lower- and upper-hex roles.

Indexing and Slicing

yulang
my c: char = "aあ🙂"[1] // char for
"aあ🙂z"[1..<3]     // "あ🙂"
"aあ🙂z"[range 1 3]

str implements Index for both int and range. Integer indexing returns a char; range indexing returns a str. Both work by character position, not byte offset.

Splicing

yulang
"aあ🙂z".splice (range 1 3) "bc"  // "abcz"

std::text::str::splice and the .splice method replace a character range with new text.

Display and .show

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

.show is the canonical conversion to str. It resolves through the Display role. The standard prelude supplies user-facing impls for primitives, unit, list, opt, result, and common tuple arities. Strings are rendered without quotes, so structural values that contain strings are meant for readable output rather than lossless inspection. Display also provides .say, which prints .show with a newline.

Define Display for a user type with the usual role machinery:

yulang
struct point { x: int, y: int }

impl Display point:
    our p.show = "(" + p.x.show + ", " + p.y.show + ")"

The result is what p.show returns and what %{p} in a string template formats.

Debug and .debug

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

.debug is the developer-facing rendering path. It resolves through the Debug role. The standard prelude provides Debug 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, so yulang run and the playground can inspect them without adding one impl per shape. Debug also provides .print and .println, which print .debug without or with a newline. Use .show / .say for user-facing text and .debug / .print / .println for structural values while inspecting programs.

Comments

yulang
// line comment

/* nested
   block comment */

-- doc line comment

---
doc block
---

// and /* ... */ are ordinary comments. -- and --- ... --- are documentation comments and may be consumed by tooling.

Yulang