percent
import std::encoding::percent; · source
Percent-encoding (RFC 3986): a byte a URI cannot carry as itself is written % and two hexadecimal digits.
Types
PercentEncoding
type struct PercentEncoding {}
The codec is stateless; its operations are statics. Encoding leaves the unreserved characters (A-Z, a-z, 0-9, -, ., _, ~) as they are and escapes every other byte, with uppercase digits. encode_component is safe as one path segment, query key or query value; encode_path keeps the / separators too, for a whole path.
import std::encoding::percent::{ PercentEncoding };
const query: String = PercentEncoding::encode_component(Str::new("a b&c")); // "a%20b%26c"
const path: String = PercentEncoding::encode_path(Str::new("/docs/a b")); // "/docs/a%20b"
decode turns every escape back into its byte; + stays +, since it means a space only in HTML form data. It refuses an escape without two hexadecimal digits (InvalidEscape, at its %) and a result that is not UTF-8 (InvalidUtf8, at the input byte that starts the bad sequence).
Static functions
static encode_component(text: Str) -> String;
static encode_component(text: Str) -> String;text with every byte but the unreserved characters escaped: safe as one path segment, query key or query value.
static encode_path(text: Str) -> String;
static encode_path(text: Str) -> String;text with every byte but the unreserved characters and / escaped: safe as a whole URI path.
static decode(text: Str) -> Result<String, DecodeError>;
static decode(text: Str) -> Result<String, DecodeError>;text with every %XX escape replaced by its byte. Refuses an escape without two hexadecimal digits (InvalidEscape, at its %) and a result that is not UTF-8 (InvalidUtf8, at the input byte that starts the bad sequence).
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 PercentEncoding {}The percent-encoding codec. 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).
namespace std::encoding::percent;Percent-encoding (RFC 3986): a byte a URI cannot carry as itself is written % and two hexadecimal digits.
type struct PercentEncoding {}The percent-encoding codec. Stateless; its operations are statics.
std::encoding::percentconst NAME: Type = value;At module level, a true compile-time constant, conventionally SCREAMING_SNAKE_CASE. (mut at module level declares mutable global state instead.)
const query: Stringtype struct String<A = GlobalAllocator> {}Owned, growable UTF-8 text. It dereferences to Str, the read-only view most text functions take, so every Str method reads a String too (s.contains(...), s.split(...)); as_str hands the view out.
static PercentEncoding::encode_component(text: Str) -> Stringtext with every byte but the unreserved characters escaped: safe as one path segment, query key or query value.
type 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.
static Str::new(text: string) -> StrConstruct from a NUL-terminated C-style string literal. The caller certifies that text is valid UTF-8 and outlives this view. The NUL terminator is not included in the view.
const path: Stringstatic PercentEncoding::encode_path(text: Str) -> Stringtext with every byte but the unreserved characters and / escaped: safe as a whole URI path.
static PercentEncoding::decode(text: Str) -> Result<String, DecodeError>text with every %XX escape replaced by its byte. Refuses an escape without two hexadecimal digits (InvalidEscape, at its %) and a result that is not UTF-8 (InvalidUtf8, at the input byte that starts the bad sequence).
DecodeErrorKind::InvalidUtf8Decoded bytes that are not well-formed UTF-8, where text was wanted.
std::encoding::errorstatic 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) text: Strtype enum Result<T, E> with [Eq, Clone, Debug] {
Ok(T);
Err(E);
}The outcome of an operation that can fail: Ok(value) on success, Err(error) on failure.
type struct DecodeError with [Eq, Debug] {
position: i64;
kind: DecodeErrorKind;
}A decoding failure: its kind, and the byte offset in the input text at which the decoder found it.
std::encoding::error