utf8
import std::encoding::utf8; · source
Reading one Unicode scalar at a byte position.
Types
Utf8
type struct Utf8 {}
Utf8::decode_at reads the scalar a position starts and Utf8::decode_before the one that ends there, each with the number of bytes it takes. Both are strict: a sequence that is not well-formed UTF-8 — a continuation byte where a scalar would start, an overlong form, a surrogate half, a value past U+10FFFF, or a sequence cut short — decodes to nothing, so a caller decides what a malformed byte means. Str::from_utf8 refuses it; Str::chars reads it as U+FFFD.
import std::encoding::utf8::{ Utf8, DecodedScalar };
function main() -> int {
const text: Str = "héllo";
match (Utf8::decode_at(text.as_bytes(), 1)) {
Option::Some(d) => { return if (d.scalar == 0xE9 && d.length == 2) { 0 } else { 1 }; }
Option::None => { return 1; }
}
}
Static functions
static decode_at(bytes: &Slice<u8>, index: i64) -> Option<DecodedScalar>;
static decode_at(bytes: &Slice<u8>, index: i64) -> Option<DecodedScalar>;The scalar whose encoding starts at byte index of bytes, or None at or past the end, before the start, or where the bytes from index are not one well-formed sequence. [0xC3, 0xA9] at 0 is U+00E9 over 2 bytes; at 1, a continuation byte, it is None.
static decode_before(bytes: &Slice<u8>, index: i64) -> Option<DecodedScalar>;
static decode_before(bytes: &Slice<u8>, index: i64) -> Option<DecodedScalar>;The scalar whose encoding ends just before byte index of bytes, or None at or before the start, past the end, or where the bytes before index do not end one well-formed sequence. [0x61, 0xC3, 0xA9] before 3 is U+00E9 over 2 bytes; before 2 it is None.
DecodedScalar
type struct DecodedScalar {
scalar: u32;
length: i64;
}
One decoded scalar: its value, and how many bytes of the input it took.
Properties
| Property | Type | Notes |
|---|---|---|
scalar | u32 | The Unicode scalar value, at most U+10FFFF and never a surrogate. |
length | i64 | The bytes its encoding takes: 1 to 4. |
type struct Name { ... }
type enum Name { ... }
type trait Name { ... }
type union Name { ... }Begins a type declaration; the keyword that follows says which kind. On its own, type Name = Existing; declares an alias.
type struct Name {
field: Type;
method(&this) -> Ret { ... }
}A value type with named fields and optional methods. Structs live on the stack and are passed by value. Fields are public by default and may carry = default values; methods take their receiver as &this, mut &this, or this.
type struct Utf8 {}The UTF-8 decoder. Stateless; its operations are statics.
import a::b;
import a::b::{ X, Y };
import a::b as c;
import a::b::*;Brings a module, or selected items from it, into scope. One path per declaration: use the brace form for several items, as to alias, and * for everything public (prefer the brace form; wildcards invite name collisions).
type struct DecodedScalar {
scalar: u32;
length: i64;
}One scalar read from UTF-8 bytes, and how many bytes encode it.
std::encoding::utf8function name<T>(param: Type) -> Ret { ... }Declares a function. Parameters are written name: Type and are never inferred; the return type follows -> and defaults to void. Declaration order does not matter: every signature is collected before any body is checked, so functions may call each other freely and recurse.
function main() -> intconst name: Type = value;An immutable binding: it cannot be reassigned after initialisation. The annotation may be omitted when an initialiser is present - the binding takes the initialiser's concrete type, inferred locally. Mutability is opt-in through mut.
const text: Strtype struct Str {}A read-only view of UTF-8 text owned elsewhere: a string literal, a String, a file's bytes. Copying a Str copies the view, not the text, and it is valid only while what it views is. Str::new("text") views a literal; String.as_str() views a String.
match (subject) {
Pattern(x) => { ... }
_ => { ... }
}The primary discriminator: branches on enum variants, integers, ranges, constants, and tuple and destructuring patterns, such as (0, 1..8) or Option::Some((count, _)), and the compiler rejects a match that misses a case. There is no fallthrough. As an expression it evaluates to the chosen arm's value, so every arm must produce the same type.
static Utf8::decode_at(bytes: &Slice<u8>, index: i64) -> Option<DecodedScalar>The scalar whose encoding starts at byte index of bytes, or None at or past the end, before the start, or where the bytes from index are not one well-formed sequence. [0xC3, 0xA9] at 0 is U+00E9 over 2 bytes; at 1, a continuation byte, it is None.
const text: StrStr::as_bytes(&this) -> &Slice<u8>Underlying byte view. Use this for byte-granularity algorithms (hashing, equality, substring search).
std::collections::strtype enum Option<T> with [Eq, Clone, Debug] {
Some(T);
None;
}A value of type T that may be absent: Some(value) or None.
Option::Some(T)d: DecodedScalarpattern => { body }Separates a match arm's pattern from its body. Arms do not fall through.
return value;Leaves the current function with the given value. A function whose return type is not void must return one; a bare return; is for void. In an async function it completes the future with the value.
if (condition) { ... } else if (condition) { ... } else { ... }Branches on a parenthesised boolean condition. Bodies are always braced; there is no single-statement form. Used as an expression, both arms are required and must produce the same type: const sign = if (n < 0) { -1 } else { 1 };.
d: DecodedScalarDecodedScalar.scalar: u32The Unicode scalar value, at most U+10FFFF and never a surrogate.
std::encoding::utf8if (condition) { ... } else { ... }The branch taken when every preceding if condition is false; else if chains another condition. In expression form the else arm is mandatory, so the expression always has a value.
Option::Nonetype struct DecodedScalar {
scalar: u32;
length: i64;
}One scalar read from UTF-8 bytes, and how many bytes encode it.
DecodedScalar.scalar: u32The Unicode scalar value, at most U+10FFFF and never a surrogate.
DecodedScalar.length: i64The bytes its encoding takes: 1 to 4.
static Utf8::decode_before(bytes: &Slice<u8>, index: i64) -> Option<DecodedScalar>The scalar whose encoding ends just before byte index of bytes, or None at or before the start, past the end, or where the bytes before index do not end one well-formed sequence. [0x61, 0xC3, 0xA9] before 3 is U+00E9 over 2 bytes; before 2 it is None.
static Str::from_utf8(bytes: &Slice<u8>) -> Result<Str, Utf8Error>Borrow bytes as a Str if they are well-formed UTF-8: the checked constructor, where from and from_raw take the caller's word for it. Overlong forms, surrogate halves, scalars above U+10FFFF and a sequence cut short are refused (Utf8::decode_at decides), and the error says where.
Str::chars(&this) -> implement Iterator<char>Iterator over the Unicode scalar values of this string, decoding UTF-8 on the fly. Each scalar is yielded as a char, so the character methods read it (c.is_digit()), and c as u32 is its code point. This is the scalar-level counterpart to byte_at/as_bytes, which stay byte-level.
static name(param: Type) -> Ret { ... }A method that belongs to the type rather than an instance, called as Type::name(..). For structs, static new(..) returning a struct literal is the idiomatic constructor.
(parameter) bytes: &Slice<u8>type struct Slice<T> {}The elements are reached through ptr, declared read-only: through a mut &Slice<T> it reads as a writable pointer, through a &Slice<T> as the read-only one it is.
(parameter) index: i64