Explorar o código

Auto merge of #212 - servo:docs, r=KiChjang

Add more doc examples.

<!-- Reviewable:start -->
This change is [<img src="https://reviewable.io/review_button.svg" height="35" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/rust-url/212)
<!-- Reviewable:end -->
bors-servo %!s(int64=10) %!d(string=hai) anos
pai
achega
52e58503e5
Modificáronse 1 ficheiros con 30 adicións e 0 borrados
  1. 30 0
      src/lib.rs

+ 30 - 0
src/lib.rs

@@ -423,6 +423,15 @@ impl Url {
     }
 
     /// Return the scheme of this URL, lower-cased, as an ASCII string without the ':' delimiter.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use url::Url;
+    ///
+    /// let url = Url::parse("file:///tmp/foo").unwrap();
+    /// assert_eq!(url.scheme(), "file");
+    /// ```
     #[inline]
     pub fn scheme(&self) -> &str {
         self.slice(..self.scheme_end)
@@ -460,6 +469,9 @@ impl Url {
     /// let url = Url::parse("ftp://rms@example.com").unwrap();
     /// assert_eq!(url.username(), "rms");
     ///
+    /// let url = Url::parse("ftp://:secret123@example.com").unwrap();
+    /// assert_eq!(url.username(), "");
+    ///
     /// let url = Url::parse("https://example.com").unwrap();
     /// assert_eq!(url.username(), "");
     /// ```
@@ -472,6 +484,24 @@ impl Url {
     }
 
     /// Return the password for this URL, if any, as a percent-encoded ASCII string.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use url::Url;
+    ///
+    /// let url = Url::parse("ftp://rms:secret123@example.com").unwrap();
+    /// assert_eq!(url.password(), Some("secret123"));
+    ///
+    /// let url = Url::parse("ftp://:secret123@example.com").unwrap();
+    /// assert_eq!(url.password(), Some("secret123"));
+    ///
+    /// let url = Url::parse("ftp://rms@example.com").unwrap();
+    /// assert_eq!(url.password(), None);
+    ///
+    /// let url = Url::parse("https://example.com").unwrap();
+    /// assert_eq!(url.password(), None);
+    /// ```
     pub fn password(&self) -> Option<&str> {
         // This ':' is not the one marking a port number since a host can not be empty.
         // (Except for file: URLs, which do not have port numbers.)