Skip to content

Commit 695351b

Browse files
author
bors-servo
authored
Auto merge of #481 - BradyMcd:port-documentation, r=SimonSapin
Port documentation Just a simple addition to the documentation. The fact that default port numbers are automatically elided makes perfect sense, but it caught me off and I found wasn't terribly well reflected in the documentation so I added a callout and an example on `port( )` and `set_port( )` function documentations. <!-- 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/481) <!-- Reviewable:end -->
2 parents 788dffb + d55d46b commit 695351b

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/lib.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -918,6 +918,9 @@ impl Url {
918918

919919
/// Return the port number for this URL, if any.
920920
///
921+
/// Note that default port numbers are never reflected by the serialization,
922+
/// use the `port_or_known_default()` method if you want a default port number returned.
923+
///
921924
/// # Examples
922925
///
923926
/// ```
@@ -928,6 +931,9 @@ impl Url {
928931
/// let url = Url::parse("https://example.com")?;
929932
/// assert_eq!(url.port(), None);
930933
///
934+
/// let url = Url::parse("https://example.com:443/")?;
935+
/// assert_eq!(url.port(), None);
936+
///
931937
/// let url = Url::parse("ssh://example.com:22")?;
932938
/// assert_eq!(url.port(), Some(22));
933939
/// # Ok(())
@@ -1427,6 +1433,8 @@ impl Url {
14271433

14281434
/// Change this URL’s port number.
14291435
///
1436+
/// Note that default port numbers are not reflected in the serialization.
1437+
///
14301438
/// If this URL is cannot-be-a-base, does not have a host, or has the `file` scheme;
14311439
/// do nothing and return `Err`.
14321440
///
@@ -1449,6 +1457,22 @@ impl Url {
14491457
/// # run().unwrap();
14501458
/// ```
14511459
///
1460+
/// Known default port numbers are not reflected:
1461+
///
1462+
/// ```rust
1463+
/// use url::Url;
1464+
/// # use std::error::Error;
1465+
///
1466+
/// # fn run() -> Result<(), Box<Error>> {
1467+
/// let mut url = Url::parse("https://example.org/")?;
1468+
///
1469+
/// url.set_port(Some(443)).map_err(|_| "cannot be base")?;
1470+
/// assert!(url.port().is_none());
1471+
/// # Ok(())
1472+
/// # }
1473+
/// # run().unwrap();
1474+
/// ```
1475+
///
14521476
/// Cannot set port for cannot-be-a-base URLs:
14531477
///
14541478
/// ```

0 commit comments

Comments
 (0)