From 3d1388f514ab18ad798251f511c54097ba5c60ca Mon Sep 17 00:00:00 2001 From: Ivan Tham Date: Fri, 7 Aug 2020 14:16:52 +0800 Subject: [PATCH] Add more examples to Path ends_with We faced a footgun when using ends_with to check extension, showing an example could prevent that. --- library/std/src/path.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/library/std/src/path.rs b/library/std/src/path.rs index 392c815ef28..bc24c1e65c8 100644 --- a/library/std/src/path.rs +++ b/library/std/src/path.rs @@ -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>(&self, child: P) -> bool {