@@ -915,7 +915,9 @@ impl Url {
915
915
}
916
916
}
917
917
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.
919
921
///
920
922
/// # Examples
921
923
///
@@ -927,6 +929,9 @@ impl Url {
927
929
/// let url = Url::parse("https://example.com")?;
928
930
/// assert_eq!(url.port(), None);
929
931
///
932
+ /// let url = Url::parse( "https://example.com:443/" )?;
933
+ /// assert_eq!( url.port( ), None );
934
+ ///
930
935
/// let url = Url::parse("ssh://example.com:22")?;
931
936
/// assert_eq!(url.port(), Some(22));
932
937
/// # Ok(())
@@ -1424,7 +1429,8 @@ impl Url {
1424
1429
self . serialization . push_str ( after_path)
1425
1430
}
1426
1431
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.
1428
1434
///
1429
1435
/// If this URL is cannot-be-a-base, does not have a host, or has the `file` scheme;
1430
1436
/// do nothing and return `Err`.
@@ -1448,6 +1454,21 @@ impl Url {
1448
1454
/// # run().unwrap();
1449
1455
/// ```
1450
1456
///
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
+ ///
1451
1472
/// Cannot set port for cannot-be-a-base URLs:
1452
1473
///
1453
1474
/// ```
0 commit comments