Skip to content

Commit 2fc6bd5

Browse files
committed
Added documentation of default port elision
1 parent a1d8c88 commit 2fc6bd5

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

src/lib.rs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -915,7 +915,9 @@ impl Url {
915915
}
916916
}
917917

918-
/// Return the port number for this URL, if any.
918+
/// Return the port number for this URL, if any. Note that default port numbers are never reflected
919+
/// by the serialization, use the `port_or_known_default()` method if you want a default port
920+
/// number returned.
919921
///
920922
/// # Examples
921923
///
@@ -927,6 +929,9 @@ impl Url {
927929
/// let url = Url::parse("https://example.com")?;
928930
/// assert_eq!(url.port(), None);
929931
///
932+
/// let url = Url::parse( "https://example.com:443/" )?;
933+
/// assert_eq!( url.port( ), None );
934+
///
930935
/// let url = Url::parse("ssh://example.com:22")?;
931936
/// assert_eq!(url.port(), Some(22));
932937
/// # Ok(())
@@ -1424,7 +1429,8 @@ impl Url {
14241429
self.serialization.push_str(after_path)
14251430
}
14261431

1427-
/// Change this URL’s port number.
1432+
/// Change this URL’s port number. Note that default port numbers are not reflected in the
1433+
/// serialization.
14281434
///
14291435
/// If this URL is cannot-be-a-base, does not have a host, or has the `file` scheme;
14301436
/// do nothing and return `Err`.
@@ -1448,6 +1454,21 @@ impl Url {
14481454
/// # run().unwrap();
14491455
/// ```
14501456
///
1457+
/// Known default port numbers are not reflected:
1458+
///
1459+
/// ```rust
1460+
/// use url::Url;
1461+
/// # use std::error::Error;
1462+
///
1463+
/// # fn run( ) -> Result< ( ), Box< Error > > {
1464+
/// let mut url = Url::parse( "https://example.org/" )?;
1465+
///
1466+
/// url.set_port( Some( 443 ) ).map_err( |_| "cannot be base" )?;
1467+
/// assert!( url.port( ).is_none( ) );
1468+
/// }
1469+
/// # run( ).unwrap( );
1470+
/// ```
1471+
///
14511472
/// Cannot set port for cannot-be-a-base URLs:
14521473
///
14531474
/// ```

0 commit comments

Comments
 (0)