Skip to content

Commit a4f781e

Browse files
committed
Auto merge of #31963 - barosl:rename-doc, r=alexcrichton
Describe more platform-specific behaviors of `std::fs::rename` I did some tests myself regarding the situation when both `from` and `to` exist, and the results were: On Linux: `from` | `to` | Result ---- | ---- | ---- Directory | Directory | Ok Directory | File | Error File | Directory | Error File | File | Ok On Windows: `from` | `to` | Result ---- | ---- | ---- Directory | Directory | Error Directory | File | Ok File | Directory | Error File | File | Ok This is a bit against the official MSDN documentation, which says "(`MOVEFILE_REPLACE_EXISTING`) cannot be used if `lpNewFileName` or `lpExistingFileName` names a directory." As evidenced above, `lpExistingFileName` *can* be a directory. I also mentioned the atomicity of the operation. Fixes #31301.
2 parents 9330006 + bcbc9e5 commit a4f781e

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

src/libstd/fs.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -965,14 +965,21 @@ pub fn symlink_metadata<P: AsRef<Path>>(path: P) -> io::Result<Metadata> {
965965
fs_imp::lstat(path.as_ref()).map(Metadata)
966966
}
967967

968-
/// Rename a file or directory to a new name.
968+
/// Rename a file or directory to a new name, replacing the original file if
969+
/// `to` already exists.
969970
///
970971
/// This will not work if the new name is on a different mount point.
971972
///
972973
/// # Platform-specific behavior
973974
///
974975
/// This function currently corresponds to the `rename` function on Unix
975976
/// and the `MoveFileEx` function with the `MOVEFILE_REPLACE_EXISTING` flag on Windows.
977+
///
978+
/// Because of this, the behavior when both `from` and `to` exist differs. On
979+
/// Unix, if `from` is a directory, `to` must also be an (empty) directory. If
980+
/// `from` is not a directory, `to` must also be not a directory. In contrast,
981+
/// on Windows, `from` can be anything, but `to` must *not* be a directory.
982+
///
976983
/// Note that, this [may change in the future][changes].
977984
/// [changes]: ../io/index.html#platform-specific-behavior
978985
///

0 commit comments

Comments
 (0)