Skip to content

Commit b0ed6d1

Browse files
author
Ariel Ben-Yehuda
authored
Rollup merge of #42955 - matklad:doc-path, r=steveklabnik
Document that `/` works as separator on Windows Hi Whenever I see code like `Path::new("./src/bin/main.rs")` or `path.ends_with("foo/bar")`, I wonder if it will work on Windows as I expect. Unfortunately, reading the current docs does not help to answer this question, because all examples are Unix-specific. However, I believe that using `/` is fine, because both Windows itself [and Rust stdlib](https://github.com/rust-lang/rust/blob/47faf1d51952ecd9d4c8a7325332fba34fbe00bd/src/libstd/sys/windows/path.rs#L26) do treat it as a file separator, and because it is [actually used](https://github.com/rust-lang/cargo/blob/abf01e1eddb3145c83f71b469ea7bee37141e5e1/tests/git.rs#L579) in Cargo. So looks like we can just document it? r? @steveklabnik cc @retep998 I don't actually program for windows that much, so I might be totally wrong, and perhaps we should advise to always use (allocating) `.join` method to construct paths of more than one component?
2 parents 4f12154 + 40dec09 commit b0ed6d1

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

src/libstd/path.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ impl<'a> Prefix<'a> {
276276
/// ```
277277
/// use std::path;
278278
///
279-
/// assert!(path::is_separator('/'));
279+
/// assert!(path::is_separator('/')); // '/' works for both Unix and Windows
280280
/// assert!(!path::is_separator('❤'));
281281
/// ```
282282
#[stable(feature = "rust1", since = "1.0.0")]
@@ -1499,9 +1499,9 @@ impl AsRef<OsStr> for PathBuf {
14991499
/// A slice of a path (akin to [`str`]).
15001500
///
15011501
/// This type supports a number of operations for inspecting a path, including
1502-
/// breaking the path into its components (separated by `/` or `\`, depending on
1503-
/// the platform), extracting the file name, determining whether the path is
1504-
/// absolute, and so on.
1502+
/// breaking the path into its components (separated by `/` on Unix and by either
1503+
/// `/` or `\` on Windows), extracting the file name, determining whether the path
1504+
/// is absolute, and so on.
15051505
///
15061506
/// This is an *unsized* type, meaning that it must always be used behind a
15071507
/// pointer like `&` or [`Box`]. For an owned version of this type,
@@ -1520,10 +1520,11 @@ impl AsRef<OsStr> for PathBuf {
15201520
/// use std::path::Path;
15211521
/// use std::ffi::OsStr;
15221522
///
1523-
/// let path = Path::new("/tmp/foo/bar.txt");
1523+
/// // Note: this example does work on Windows
1524+
/// let path = Path::new("./foo/bar.txt");
15241525
///
15251526
/// let parent = path.parent();
1526-
/// assert_eq!(parent, Some(Path::new("/tmp/foo")));
1527+
/// assert_eq!(parent, Some(Path::new("./foo")));
15271528
///
15281529
/// let file_stem = path.file_stem();
15291530
/// assert_eq!(file_stem, Some(OsStr::new("bar")));

0 commit comments

Comments
 (0)