Movatterモバイル変換


[0]ホーム

URL:


Docs.rs

Crateurl

Source
Expand description

rust-url is an implementation of theURL Standardfor theRust programming language.

§URL parsing and data structures

First, URL parsing may fail for various reasons and therefore returns aResult.

useurl::{Url, ParseError};assert!(Url::parse("http://[:::1]") ==Err(ParseError::InvalidIpv6Address))

Let’s parse a valid URL and look at its components.

useurl::{Url, Host, Position};letissue_list_url = Url::parse("https://github.com/rust-lang/rust/issues?labels=E-easy&state=open")?;assert!(issue_list_url.scheme() =="https");assert!(issue_list_url.username() =="");assert!(issue_list_url.password() ==None);assert!(issue_list_url.host_str() ==Some("github.com"));assert!(issue_list_url.host() ==Some(Host::Domain("github.com")));assert!(issue_list_url.port() ==None);assert!(issue_list_url.path() =="/rust-lang/rust/issues");assert!(issue_list_url.path_segments().map(|c| c.collect::<Vec<_>>()) ==Some(vec!["rust-lang","rust","issues"]));assert!(issue_list_url.query() ==Some("labels=E-easy&state=open"));assert!(&issue_list_url[Position::BeforePath..] =="/rust-lang/rust/issues?labels=E-easy&state=open");assert!(issue_list_url.fragment() ==None);assert!(!issue_list_url.cannot_be_a_base());

Some URLs are said to becannot-be-a-base:they don’t have a username, password, host, or port,and their “path” is an arbitrary string rather than slash-separated segments:

useurl::Url;letdata_url = Url::parse("data:text/plain,Hello?World#")?;assert!(data_url.cannot_be_a_base());assert!(data_url.scheme() =="data");assert!(data_url.path() =="text/plain,Hello");assert!(data_url.path_segments().is_none());assert!(data_url.query() ==Some("World"));assert!(data_url.fragment() ==Some(""));

§Default Features

Versions<= 2.5.2 of the crate have no default features. Versions> 2.5.2 have the default feature ‘std’.If you are upgrading across this boundary and you have specifieddefault-features = false, thenyou will need to add the ‘std’ feature or the ‘alloc’ feature to your dependency.The ‘std’ feature has the same behavior as the previous versions. The ‘alloc’ featureprovides no_std support.

§Serde

Enable theserde feature to includeDeserialize andSerialize implementations forurl::Url.

§Base URL

Many contexts allow URLreferences that can be relative to abase URL:

<link rel="stylesheet" href="../main.css">

Since parsed URLs are absolute, giving a base is required for parsing relative URLs:

useurl::{Url, ParseError};assert!(Url::parse("../main.css") ==Err(ParseError::RelativeUrlWithoutBase))

Use thejoin method on anUrl to use it as a base URL:

useurl::Url;letthis_document = Url::parse("http://servo.github.io/rust-url/url/index.html")?;letcss_url = this_document.join("../main.css")?;assert_eq!(css_url.as_str(),"http://servo.github.io/rust-url/main.css");

§Feature:serde

If you enable theserde feature,Url will implementserde::Serialize andserde::Deserialize.Seeserde documentation for more information.

url = { version = "2", features = ["serde"] }

§Feature:debugger_visualizer

If you enable thedebugger_visualizer feature, theurl crate will includeanatvis fileforVisual Studio that allows you to viewUrl objects in the debugger.

This feature requires Rust 1.71 or later.

url = { version = "2", features = ["debugger_visualizer"] }

Re-exports§

pub useform_urlencoded;

Structs§

OpaqueOrigin
Opaque identifier for URLs that have file or other schemes
ParseOptions
Full configuration for the URL parser.
PathSegmentsMut
Exposes methods to manipulate the path of an URL that is not cannot-be-base.
Url
A parsed URL record.
UrlQuery
Implementation detail ofUrl::query_pairs_mut. Typically not used directly.

Enums§

Host
The host name of an URL.
Origin
The origin of an URL
ParseError
Errors that can occur during parsing.
Position
Indicates a position within a URL based on its components.
SyntaxViolation
Non-fatal syntax violations that can occur during parsing.

Type Aliases§

EncodingOverride

[8]ページ先頭

©2009-2025 Movatter.jp