]> git.lizzy.rs Git - rust.git/commitdiff
Add more examples to Path ends_with
authorIvan Tham <pickfire@riseup.net>
Fri, 7 Aug 2020 06:16:52 +0000 (14:16 +0800)
committerGitHub <noreply@github.com>
Fri, 7 Aug 2020 06:16:52 +0000 (14:16 +0800)
We faced a footgun when using ends_with to check extension,
showing an example could prevent that.

library/std/src/path.rs

index 392c815ef2803a5002d03564d49e5f330c55e884..bc24c1e65c824a65be135921502bb3f9d036d8c2 100644 (file)
@@ -2112,6 +2112,11 @@ fn _starts_with(&self, base: &Path) -> bool {
     /// 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 {