Переглянути джерело

Auto merge of #239 - frewsxcv:doc-examples, r=SimonSapin

Add a few doc examples.

<!-- Reviewable:start -->
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/rust-url/239)
<!-- Reviewable:end -->
bors-servo 10 роки тому
батько
коміт
1be1b3be4d
1 змінених файлів з 54 додано та 0 видалено
  1. 54 0
      src/lib.rs

+ 54 - 0
src/lib.rs

@@ -234,6 +234,26 @@ impl Url {
     }
     }
 
 
     /// Parse a string as an URL, with this URL as the base URL.
     /// Parse a string as an URL, with this URL as the base URL.
+    ///
+    /// # Examples
+    ///
+    /// ```rust
+    /// use url::Url;
+    ///
+    /// let url = Url::parse("https://example.net").unwrap();
+    /// let url = url.join("foo").unwrap();
+    /// assert_eq!(url.as_str(), "https://example.net/foo");
+    /// ```
+    ///
+    /// Trailing slashes are not preserved:
+    ///
+    /// ```rust
+    /// use url::Url;
+    ///
+    /// let url = Url::parse("https://example.net/foo/").unwrap();
+    /// let url = url.join("bar").unwrap();
+    /// assert_eq!(url.as_str(), "https://example.net/foo/bar");
+    /// ```
     #[inline]
     #[inline]
     pub fn join(&self, input: &str) -> Result<Url, ::ParseError> {
     pub fn join(&self, input: &str) -> Result<Url, ::ParseError> {
         Url::options().base_url(Some(self)).parse(input)
         Url::options().base_url(Some(self)).parse(input)
@@ -251,6 +271,16 @@ impl Url {
     /// Return the serialization of this URL.
     /// Return the serialization of this URL.
     ///
     ///
     /// This is fast since that serialization is already stored in the `Url` struct.
     /// This is fast since that serialization is already stored in the `Url` struct.
+    ///
+    /// # Examples
+    ///
+    /// ```rust
+    /// use url::Url;
+    ///
+    /// let url_str = "https://example.net/";
+    /// let url = Url::parse(url_str).unwrap();
+    /// assert_eq!(url.as_str(), url_str);
+    /// ```
     #[inline]
     #[inline]
     pub fn as_str(&self) -> &str {
     pub fn as_str(&self) -> &str {
         &self.serialization
         &self.serialization
@@ -259,6 +289,16 @@ impl Url {
     /// Return the serialization of this URL.
     /// Return the serialization of this URL.
     ///
     ///
     /// This consumes the `Url` and takes ownership of the `String` stored in it.
     /// This consumes the `Url` and takes ownership of the `String` stored in it.
+    ///
+    /// # Examples
+    ///
+    /// ```rust
+    /// use url::Url;
+    ///
+    /// let url_str = "https://example.net/";
+    /// let url = Url::parse(url_str).unwrap();
+    /// assert_eq!(url.into_string(), url_str);
+    /// ```
     #[inline]
     #[inline]
     pub fn into_string(self) -> String {
     pub fn into_string(self) -> String {
         self.serialization
         self.serialization
@@ -890,6 +930,20 @@ impl Url {
     ///
     ///
     /// If this URL is cannot-be-a-base, does not have a host, or has the `file` scheme;
     /// If this URL is cannot-be-a-base, does not have a host, or has the `file` scheme;
     /// do nothing and return `Err`.
     /// do nothing and return `Err`.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use url::Url;
+    ///
+    /// let mut url = Url::parse("ssh://example.net:2048/").unwrap();
+    ///
+    /// url.set_port(Some(4096)).unwrap();
+    /// assert_eq!(url.as_str(), "ssh://example.net:4096/");
+    ///
+    /// url.set_port(None).unwrap();
+    /// assert_eq!(url.as_str(), "ssh://example.net/");
+    /// ```
     pub fn set_port(&mut self, mut port: Option<u16>) -> Result<(), ()> {
     pub fn set_port(&mut self, mut port: Option<u16>) -> Result<(), ()> {
         if !self.has_host() || self.scheme() == "file" {
         if !self.has_host() || self.scheme() == "file" {
             return Err(())
             return Err(())