Standard library
UTF-8
Strict UTF-8 validation and decode (not lossy str.from_bytes).
Introduction
Strict UTF-8 validation and decode (not lossy str.from_bytes). Import std/encoding/utf8 to bind the module as utf8. Call free functions as utf8.name(...). Struct methods use a receiver value.
/ std/encoding/utf8
This page is the package reference for std/encoding/utf8. Private helpers used only by co-located tests are not listed.
Functions
Free functions on utf8. Each function has a short description, an example, then parameters and return shape.
valid
Reports whether b is well-formed UTF-8. Strings are always valid. Invalid sequences are false. Call form: utf8.valid(b).
/ std/encoding/utf8
$ ok = utf8.valid(b"hi")
Parameters: b: bytes or string. Returns: Boolean.
decode
Decodes well-formed UTF-8 to a string. Invalid sequences fail. Use str.from_bytes for lossy decode. Call form: utf8.decode(b).
/ std/encoding/utf8
| utf8.decode(b"hi") {
$ t { }
! e { }
}Parameters: b: bytes or string. Returns: Result. Ok arm: string. Err arm: "invalid utf-8".