Add more examples to Path ends_with

We faced a footgun when using ends_with to check extension,
showing an example could prevent that.
This commit is contained in:
Ivan Tham 2020-08-07 14:16:52 +08:00 committed by GitHub
parent 63e34422bb
commit 3d1388f514
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 5 additions and 0 deletions

View File

@ -2112,6 +2112,11 @@ impl Path {
/// let path = Path::new("/etc/passwd");
///
/// assert!(path.ends_with("passwd"));
/// assert!(path.ends_with("etc/passwd"));
/// assert!(path.ends_with("/etc/passwd"));
///
/// assert!(!path.ends_with("/passwd"));
/// assert!(!path.ends_with("wd")); // use .extension() instead
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn ends_with<P: AsRef<Path>>(&self, child: P) -> bool {