fragment() - > Option<string>- Модуль
- std.
url → - Получатель
- URL
- Минимальное число аргументов
- 0
- Максимальное число аргументов
- 0
Declaration and parameters
СсылкаДоступность
Включено по умолчанию · Не определено
Import std.url in language mode 5.21.
The current compiler selects std/url.tpz for this language mode. Availability in other released editions and inclusion in each installed distribution are Pending.
Получатель
URL
Ограничения параметров
URL.scheme returns the lowercase scheme without its delimiter. URL.host returns Some containing the serialized host without user information or a port. IPv6 hosts include their square brackets.
A URL whose host is absent returns None() from URL.host. A present empty host returns Some containing an empty string. A local file URL can have that empty host.
URL.path returns the serialized path without the query or fragment. A hierarchical path retains its leading slash. An opaque path has no implied slash. Percent escapes stay encoded so an escaped slash remains path data.
URL.fragment returns None() when there is no fragment delimiter. An explicit empty fragment returns Some containing an empty string. A present fragment excludes its delimiter and retains percent encoding.
Scheme, host, path, and fragment inspection cannot fail after construction. Each receiver is evaluated once. The returned strings are immutable and inspection has no external effects.
Read URL.query for decoded query fields. URL.path and URL.fragment do not apply query decoding. In particular, a plus sign in those components remains a plus sign.
Связанное правило · Read serialized URL components and their absence →Результат
The declared result type is Option<string>.
Эффекты
Ошибки
Result and effects
СсылкаReturns the optional encoded fragment and preserves a present empty fragment.
Resolved contracts
СсылкаRelated entries
СсылкаRead serialized URL components and their absence
СсылкаRead serialized URL components and their absence
URL.scheme returns the lowercase scheme without its delimiter. URL.host returns Some containing the serialized host without user information or a port. IPv6 hosts include their square brackets.
A URL whose host is absent returns None() from URL.host. A present empty host returns Some containing an empty string. A local file URL can have that empty host.
URL.path returns the serialized path without the query or fragment. A hierarchical path retains its leading slash. An opaque path has no implied slash. Percent escapes stay encoded so an escaped slash remains path data.
URL.fragment returns None() when there is no fragment delimiter. An explicit empty fragment returns Some containing an empty string. A present fragment excludes its delimiter and retains percent encoding.
Scheme, host, path, and fragment inspection cannot fail after construction. Each receiver is evaluated once. The returned strings are immutable and inspection has no external effects.
Read URL.query for decoded query fields. URL.path and URL.fragment do not apply query decoding. In particular, a plus sign in those components remains a plus sign.
Причина
Component access must preserve delimiters as data and distinguish absence from an empty component. Encoded path and fragment values can be placed back into a URL without turning an escaped separator into structure.
Read encoded URL components without decoding delimiters
Ссылкаimport std.url { URL }import std.iomatch (URL.parse("HTTPS://EXAMPLE.COM:8443/a%2Fb?q=a+b#x%20y")) {case Ok(value) => {io.writeLine(value.scheme())match (value.host()) {case Some(host) => io.writeLine(host)case None() => io.writeLine("no host")}io.writeLine(value.path())match (value.fragment()) {case Some(fragment) => io.writeLine(fragment)case None() => io.writeLine("no fragment")}io.writeLine(value.toString())}case Err(_) => io.writeLine("parse failed")}
https
example.com
/a%2Fb
x%20y
https://example.com:8443/a%2Fb?q=a+b#x%20y
Ожидаемый результат
Ожидаемый результат
Выполнение завершается. Код завершения 0.
httpsexample.com/a%2Fbx%20yhttps://example.com:8443/a%2Fb?q=a+b#x%20y
Distinguish an absent fragment from an empty fragment
Ссылкаimport std.url { URL }import std.iofor text in (["mailto:reader@example.com", "mailto:reader@example.com#"]) {match (URL.parse(text)) {case Ok(value) => {io.writeLine(value.scheme())match (value.host()) {case None() => io.writeLine("no host")case Some(host) => io.writeLine("host={host}")}io.writeLine(value.path())match (value.fragment()) {case None() => io.writeLine("no fragment")case Some(fragment) => io.writeLine("fragment={fragment}")}io.writeLine("{value.query().length}")}case Err(_) => io.writeLine("parse failed")}}
mailto
no host
reader@example.com
no fragment
0
mailto
no host
reader@example.com
fragment=
0
Ожидаемый результат
Ожидаемый результат
Выполнение завершается. Код завершения 0.
mailtono hostreader@example.comno fragment0mailtono hostreader@example.comfragment=0
Read IPv6 and file hosts without losing authority data
Ссылкаimport std.url { URL }import std.iofor text in (["https://u:p@[2001:DB8::1]:443/", "file:///tmp/a"]) {match (URL.parse(text)) {case Ok(value) => {match (value.host()) {case Some(host) => io.writeLine("host={host}")case None() => io.writeLine("no host")}io.writeLine(value.toString())}case Err(_) => io.writeLine("parse failed")}}
host=[2001:db8::1]
https://u:p@[2001:db8::1]/
host=
file:///tmp/a
Ожидаемый результат
Ожидаемый результат
Выполнение завершается. Код завершения 0.
host=[2001:db8::1]https://u:p@[2001:db8::1]/host=file:///tmp/a
Serialize a Unicode domain, path, and fragment
Ссылкаimport std.url { URL }import std.iomatch (URL.parse("https://bücher.example/한#α")) {case Ok(value) => {match (value.host()) {case Some(host) => io.writeLine(host)case None() => io.writeLine("no host")}io.writeLine(value.path())match (value.fragment()) {case Some(fragment) => io.writeLine(fragment)case None() => io.writeLine("no fragment")}io.writeLine(value.toString())}case Err(_) => io.writeLine("parse failed")}
xn--bcher-kva.example
/%ED%95%9C
%CE%B1
https://xn--bcher-kva.example/%ED%95%9C#%CE%B1
Ожидаемый результат
Ожидаемый результат
Выполнение завершается. Код завершения 0.
xn--bcher-kva.example/%ED%95%9C%CE%B1https://xn--bcher-kva.example/%ED%95%9C#%CE%B1