Skip to content

Commit 3d1388f

Browse files
authored
Add more examples to Path ends_with
We faced a footgun when using ends_with to check extension, showing an example could prevent that.
1 parent 63e3442 commit 3d1388f

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

library/std/src/path.rs

+5
Original file line numberDiff line numberDiff line change
@@ -2112,6 +2112,11 @@ impl Path {
21122112
/// let path = Path::new("/etc/passwd");
21132113
///
21142114
/// assert!(path.ends_with("passwd"));
2115+
/// assert!(path.ends_with("etc/passwd"));
2116+
/// assert!(path.ends_with("/etc/passwd"));
2117+
///
2118+
/// assert!(!path.ends_with("/passwd"));
2119+
/// assert!(!path.ends_with("wd")); // use .extension() instead
21152120
/// ```
21162121
#[stable(feature = "rust1", since = "1.0.0")]
21172122
pub fn ends_with<P: AsRef<Path>>(&self, child: P) -> bool {

0 commit comments

Comments
 (0)