Skip to content

Commit 881ddba

Browse files
Expand PathBuf documentation to show some strange behaviour
1 parent ba956ef commit 881ddba

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

library/std/src/path.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,6 +1156,20 @@ impl FusedIterator for Ancestors<'_> {}
11561156
/// ```
11571157
///
11581158
/// Which method works best depends on what kind of situation you're in.
1159+
///
1160+
/// Note that `PathBuf`` does not sanitize arguments, and will build
1161+
/// paths from strings which include separators, for example:
1162+
///
1163+
/// use std::path::PathBuf;
1164+
///
1165+
/// let mut path = PathBuf::new();
1166+
///
1167+
/// path.push(r"C:\");
1168+
/// path.push("windows");
1169+
/// path.push(r"..\otherdir");
1170+
/// path.push("system32");
1171+
///
1172+
/// path.set_extension(r"\..\temp");
11591173
#[cfg_attr(not(test), rustc_diagnostic_item = "PathBuf")]
11601174
#[stable(feature = "rust1", since = "1.0.0")]
11611175
pub struct PathBuf {
@@ -1379,6 +1393,8 @@ impl PathBuf {
13791393
/// `file_name`. The new path will be a sibling of the original path.
13801394
/// (That is, it will have the same parent.)
13811395
///
1396+
/// The argument is not sanitized, so can include separators.
1397+
///
13821398
/// [`self.file_name`]: Path::file_name
13831399
/// [`pop`]: PathBuf::pop
13841400
///
@@ -1399,6 +1415,12 @@ impl PathBuf {
13991415
///
14001416
/// buf.set_file_name("baz");
14011417
/// assert!(buf == PathBuf::from("/baz"));
1418+
///
1419+
/// buf.set_file_name("../b/c.txt");
1420+
/// assert!(buf == PathBuf::from("/../b/c.txt"));
1421+
///
1422+
/// buf.set_file_name("baz");
1423+
/// assert!(buf == PathBuf::from("/../b/baz"));
14021424
/// ```
14031425
#[stable(feature = "rust1", since = "1.0.0")]
14041426
pub fn set_file_name<S: AsRef<OsStr>>(&mut self, file_name: S) {
@@ -1434,6 +1456,8 @@ impl PathBuf {
14341456
/// If the file stem contains internal dots and `extension` is empty, part
14351457
/// of the old file stem will be considered the new [`self.extension`].
14361458
///
1459+
/// The new `extension` is not sanitized, so may include separators.
1460+
///
14371461
/// See the examples below.
14381462
///
14391463
/// [`self.file_name`]: Path::file_name
@@ -1463,6 +1487,9 @@ impl PathBuf {
14631487
///
14641488
/// p.set_extension("");
14651489
/// assert_eq!(Path::new("/feel/the"), p.as_path());
1490+
///
1491+
/// p.set_extension("/darkest.cookie");
1492+
/// assert_eq!(Path::new("/feel/the./darkest.cookie"), p.as_path());
14661493
/// ```
14671494
#[stable(feature = "rust1", since = "1.0.0")]
14681495
pub fn set_extension<S: AsRef<OsStr>>(&mut self, extension: S) -> bool {

0 commit comments

Comments
 (0)